remove malta-mtd.c and fix MAINTAINERS
[linux-2.6/zen-sources.git] / drivers / net / xen-netfront.c
blob6d017adc914ac18c5389fb550b53ed5aa09b3246
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 <net/ip.h>
45 #include <xen/xenbus.h>
46 #include <xen/events.h>
47 #include <xen/page.h>
48 #include <xen/grant_table.h>
50 #include <xen/interface/io/netif.h>
51 #include <xen/interface/memory.h>
52 #include <xen/interface/grant_table.h>
54 static struct ethtool_ops xennet_ethtool_ops;
56 struct netfront_cb {
57 struct page *page;
58 unsigned offset;
61 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
63 #define RX_COPY_THRESHOLD 256
65 #define GRANT_INVALID_REF 0
67 #define NET_TX_RING_SIZE __RING_SIZE((struct xen_netif_tx_sring *)0, PAGE_SIZE)
68 #define NET_RX_RING_SIZE __RING_SIZE((struct xen_netif_rx_sring *)0, PAGE_SIZE)
69 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
71 struct netfront_info {
72 struct list_head list;
73 struct net_device *netdev;
75 struct napi_struct napi;
77 unsigned int evtchn;
78 struct xenbus_device *xbdev;
80 spinlock_t tx_lock;
81 struct xen_netif_tx_front_ring tx;
82 int tx_ring_ref;
85 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
86 * are linked from tx_skb_freelist through skb_entry.link.
88 * NB. Freelist index entries are always going to be less than
89 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
90 * greater than PAGE_OFFSET: we use this property to distinguish
91 * them.
93 union skb_entry {
94 struct sk_buff *skb;
95 unsigned long link;
96 } tx_skbs[NET_TX_RING_SIZE];
97 grant_ref_t gref_tx_head;
98 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
99 unsigned tx_skb_freelist;
101 spinlock_t rx_lock ____cacheline_aligned_in_smp;
102 struct xen_netif_rx_front_ring rx;
103 int rx_ring_ref;
105 /* Receive-ring batched refills. */
106 #define RX_MIN_TARGET 8
107 #define RX_DFL_MIN_TARGET 64
108 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
109 unsigned rx_min_target, rx_max_target, rx_target;
110 struct sk_buff_head rx_batch;
112 struct timer_list rx_refill_timer;
114 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
115 grant_ref_t gref_rx_head;
116 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
118 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
119 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
120 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
123 struct netfront_rx_info {
124 struct xen_netif_rx_response rx;
125 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
128 static void skb_entry_set_link(union skb_entry *list, unsigned short id)
130 list->link = id;
133 static int skb_entry_is_link(const union skb_entry *list)
135 BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
136 return ((unsigned long)list->skb < PAGE_OFFSET);
140 * Access macros for acquiring freeing slots in tx_skbs[].
143 static void add_id_to_freelist(unsigned *head, union skb_entry *list,
144 unsigned short id)
146 skb_entry_set_link(&list[id], *head);
147 *head = id;
150 static unsigned short get_id_from_freelist(unsigned *head,
151 union skb_entry *list)
153 unsigned int id = *head;
154 *head = list[id].link;
155 return id;
158 static int xennet_rxidx(RING_IDX idx)
160 return idx & (NET_RX_RING_SIZE - 1);
163 static struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
164 RING_IDX ri)
166 int i = xennet_rxidx(ri);
167 struct sk_buff *skb = np->rx_skbs[i];
168 np->rx_skbs[i] = NULL;
169 return skb;
172 static grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
173 RING_IDX ri)
175 int i = xennet_rxidx(ri);
176 grant_ref_t ref = np->grant_rx_ref[i];
177 np->grant_rx_ref[i] = GRANT_INVALID_REF;
178 return ref;
181 #ifdef CONFIG_SYSFS
182 static int xennet_sysfs_addif(struct net_device *netdev);
183 static void xennet_sysfs_delif(struct net_device *netdev);
184 #else /* !CONFIG_SYSFS */
185 #define xennet_sysfs_addif(dev) (0)
186 #define xennet_sysfs_delif(dev) do { } while (0)
187 #endif
189 static int xennet_can_sg(struct net_device *dev)
191 return dev->features & NETIF_F_SG;
195 static void rx_refill_timeout(unsigned long data)
197 struct net_device *dev = (struct net_device *)data;
198 struct netfront_info *np = netdev_priv(dev);
199 netif_rx_schedule(dev, &np->napi);
202 static int netfront_tx_slot_available(struct netfront_info *np)
204 return ((np->tx.req_prod_pvt - np->tx.rsp_cons) <
205 (TX_MAX_TARGET - MAX_SKB_FRAGS - 2));
208 static void xennet_maybe_wake_tx(struct net_device *dev)
210 struct netfront_info *np = netdev_priv(dev);
212 if (unlikely(netif_queue_stopped(dev)) &&
213 netfront_tx_slot_available(np) &&
214 likely(netif_running(dev)))
215 netif_wake_queue(dev);
218 static void xennet_alloc_rx_buffers(struct net_device *dev)
220 unsigned short id;
221 struct netfront_info *np = netdev_priv(dev);
222 struct sk_buff *skb;
223 struct page *page;
224 int i, batch_target, notify;
225 RING_IDX req_prod = np->rx.req_prod_pvt;
226 grant_ref_t ref;
227 unsigned long pfn;
228 void *vaddr;
229 struct xen_netif_rx_request *req;
231 if (unlikely(!netif_carrier_ok(dev)))
232 return;
235 * Allocate skbuffs greedily, even though we batch updates to the
236 * receive ring. This creates a less bursty demand on the memory
237 * allocator, so should reduce the chance of failed allocation requests
238 * both for ourself and for other kernel subsystems.
240 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
241 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
242 skb = __netdev_alloc_skb(dev, RX_COPY_THRESHOLD + NET_IP_ALIGN,
243 GFP_ATOMIC | __GFP_NOWARN);
244 if (unlikely(!skb))
245 goto no_skb;
247 /* Align ip header to a 16 bytes boundary */
248 skb_reserve(skb, NET_IP_ALIGN);
250 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
251 if (!page) {
252 kfree_skb(skb);
253 no_skb:
254 /* Any skbuffs queued for refill? Force them out. */
255 if (i != 0)
256 goto refill;
257 /* Could not allocate any skbuffs. Try again later. */
258 mod_timer(&np->rx_refill_timer,
259 jiffies + (HZ/10));
260 break;
263 skb_shinfo(skb)->frags[0].page = page;
264 skb_shinfo(skb)->nr_frags = 1;
265 __skb_queue_tail(&np->rx_batch, skb);
268 /* Is the batch large enough to be worthwhile? */
269 if (i < (np->rx_target/2)) {
270 if (req_prod > np->rx.sring->req_prod)
271 goto push;
272 return;
275 /* Adjust our fill target if we risked running out of buffers. */
276 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
277 ((np->rx_target *= 2) > np->rx_max_target))
278 np->rx_target = np->rx_max_target;
280 refill:
281 for (i = 0; ; i++) {
282 skb = __skb_dequeue(&np->rx_batch);
283 if (skb == NULL)
284 break;
286 skb->dev = dev;
288 id = xennet_rxidx(req_prod + i);
290 BUG_ON(np->rx_skbs[id]);
291 np->rx_skbs[id] = skb;
293 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
294 BUG_ON((signed short)ref < 0);
295 np->grant_rx_ref[id] = ref;
297 pfn = page_to_pfn(skb_shinfo(skb)->frags[0].page);
298 vaddr = page_address(skb_shinfo(skb)->frags[0].page);
300 req = RING_GET_REQUEST(&np->rx, req_prod + i);
301 gnttab_grant_foreign_access_ref(ref,
302 np->xbdev->otherend_id,
303 pfn_to_mfn(pfn),
306 req->id = id;
307 req->gref = ref;
310 wmb(); /* barrier so backend seens requests */
312 /* Above is a suitable barrier to ensure backend will see requests. */
313 np->rx.req_prod_pvt = req_prod + i;
314 push:
315 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
316 if (notify)
317 notify_remote_via_irq(np->netdev->irq);
320 static int xennet_open(struct net_device *dev)
322 struct netfront_info *np = netdev_priv(dev);
324 napi_enable(&np->napi);
326 spin_lock_bh(&np->rx_lock);
327 if (netif_carrier_ok(dev)) {
328 xennet_alloc_rx_buffers(dev);
329 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
330 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
331 netif_rx_schedule(dev, &np->napi);
333 spin_unlock_bh(&np->rx_lock);
335 netif_start_queue(dev);
337 return 0;
340 static void xennet_tx_buf_gc(struct net_device *dev)
342 RING_IDX cons, prod;
343 unsigned short id;
344 struct netfront_info *np = netdev_priv(dev);
345 struct sk_buff *skb;
347 BUG_ON(!netif_carrier_ok(dev));
349 do {
350 prod = np->tx.sring->rsp_prod;
351 rmb(); /* Ensure we see responses up to 'rp'. */
353 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
354 struct xen_netif_tx_response *txrsp;
356 txrsp = RING_GET_RESPONSE(&np->tx, cons);
357 if (txrsp->status == NETIF_RSP_NULL)
358 continue;
360 id = txrsp->id;
361 skb = np->tx_skbs[id].skb;
362 if (unlikely(gnttab_query_foreign_access(
363 np->grant_tx_ref[id]) != 0)) {
364 printk(KERN_ALERT "xennet_tx_buf_gc: warning "
365 "-- grant still in use by backend "
366 "domain.\n");
367 BUG();
369 gnttab_end_foreign_access_ref(
370 np->grant_tx_ref[id], GNTMAP_readonly);
371 gnttab_release_grant_reference(
372 &np->gref_tx_head, np->grant_tx_ref[id]);
373 np->grant_tx_ref[id] = GRANT_INVALID_REF;
374 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, id);
375 dev_kfree_skb_irq(skb);
378 np->tx.rsp_cons = prod;
381 * Set a new event, then check for race with update of tx_cons.
382 * Note that it is essential to schedule a callback, no matter
383 * how few buffers are pending. Even if there is space in the
384 * transmit ring, higher layers may be blocked because too much
385 * data is outstanding: in such cases notification from Xen is
386 * likely to be the only kick that we'll get.
388 np->tx.sring->rsp_event =
389 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
390 mb(); /* update shared area */
391 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
393 xennet_maybe_wake_tx(dev);
396 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
397 struct xen_netif_tx_request *tx)
399 struct netfront_info *np = netdev_priv(dev);
400 char *data = skb->data;
401 unsigned long mfn;
402 RING_IDX prod = np->tx.req_prod_pvt;
403 int frags = skb_shinfo(skb)->nr_frags;
404 unsigned int offset = offset_in_page(data);
405 unsigned int len = skb_headlen(skb);
406 unsigned int id;
407 grant_ref_t ref;
408 int i;
410 /* While the header overlaps a page boundary (including being
411 larger than a page), split it it into page-sized chunks. */
412 while (len > PAGE_SIZE - offset) {
413 tx->size = PAGE_SIZE - offset;
414 tx->flags |= NETTXF_more_data;
415 len -= tx->size;
416 data += tx->size;
417 offset = 0;
419 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
420 np->tx_skbs[id].skb = skb_get(skb);
421 tx = RING_GET_REQUEST(&np->tx, prod++);
422 tx->id = id;
423 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
424 BUG_ON((signed short)ref < 0);
426 mfn = virt_to_mfn(data);
427 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
428 mfn, GNTMAP_readonly);
430 tx->gref = np->grant_tx_ref[id] = ref;
431 tx->offset = offset;
432 tx->size = len;
433 tx->flags = 0;
436 /* Grant backend access to each skb fragment page. */
437 for (i = 0; i < frags; i++) {
438 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
440 tx->flags |= NETTXF_more_data;
442 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
443 np->tx_skbs[id].skb = skb_get(skb);
444 tx = RING_GET_REQUEST(&np->tx, prod++);
445 tx->id = id;
446 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
447 BUG_ON((signed short)ref < 0);
449 mfn = pfn_to_mfn(page_to_pfn(frag->page));
450 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
451 mfn, GNTMAP_readonly);
453 tx->gref = np->grant_tx_ref[id] = ref;
454 tx->offset = frag->page_offset;
455 tx->size = frag->size;
456 tx->flags = 0;
459 np->tx.req_prod_pvt = prod;
462 static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
464 unsigned short id;
465 struct netfront_info *np = netdev_priv(dev);
466 struct xen_netif_tx_request *tx;
467 struct xen_netif_extra_info *extra;
468 char *data = skb->data;
469 RING_IDX i;
470 grant_ref_t ref;
471 unsigned long mfn;
472 int notify;
473 int frags = skb_shinfo(skb)->nr_frags;
474 unsigned int offset = offset_in_page(data);
475 unsigned int len = skb_headlen(skb);
477 frags += DIV_ROUND_UP(offset + len, PAGE_SIZE);
478 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
479 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
480 frags);
481 dump_stack();
482 goto drop;
485 spin_lock_irq(&np->tx_lock);
487 if (unlikely(!netif_carrier_ok(dev) ||
488 (frags > 1 && !xennet_can_sg(dev)) ||
489 netif_needs_gso(dev, skb))) {
490 spin_unlock_irq(&np->tx_lock);
491 goto drop;
494 i = np->tx.req_prod_pvt;
496 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
497 np->tx_skbs[id].skb = skb;
499 tx = RING_GET_REQUEST(&np->tx, i);
501 tx->id = id;
502 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
503 BUG_ON((signed short)ref < 0);
504 mfn = virt_to_mfn(data);
505 gnttab_grant_foreign_access_ref(
506 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
507 tx->gref = np->grant_tx_ref[id] = ref;
508 tx->offset = offset;
509 tx->size = len;
510 extra = NULL;
512 tx->flags = 0;
513 if (skb->ip_summed == CHECKSUM_PARTIAL)
514 /* local packet? */
515 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
516 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
517 /* remote but checksummed. */
518 tx->flags |= NETTXF_data_validated;
520 if (skb_shinfo(skb)->gso_size) {
521 struct xen_netif_extra_info *gso;
523 gso = (struct xen_netif_extra_info *)
524 RING_GET_REQUEST(&np->tx, ++i);
526 if (extra)
527 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
528 else
529 tx->flags |= NETTXF_extra_info;
531 gso->u.gso.size = skb_shinfo(skb)->gso_size;
532 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
533 gso->u.gso.pad = 0;
534 gso->u.gso.features = 0;
536 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
537 gso->flags = 0;
538 extra = gso;
541 np->tx.req_prod_pvt = i + 1;
543 xennet_make_frags(skb, dev, tx);
544 tx->size = skb->len;
546 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
547 if (notify)
548 notify_remote_via_irq(np->netdev->irq);
550 dev->stats.tx_bytes += skb->len;
551 dev->stats.tx_packets++;
553 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
554 xennet_tx_buf_gc(dev);
556 if (!netfront_tx_slot_available(np))
557 netif_stop_queue(dev);
559 spin_unlock_irq(&np->tx_lock);
561 return 0;
563 drop:
564 dev->stats.tx_dropped++;
565 dev_kfree_skb(skb);
566 return 0;
569 static int xennet_close(struct net_device *dev)
571 struct netfront_info *np = netdev_priv(dev);
572 netif_stop_queue(np->netdev);
573 napi_disable(&np->napi);
574 return 0;
577 static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
578 grant_ref_t ref)
580 int new = xennet_rxidx(np->rx.req_prod_pvt);
582 BUG_ON(np->rx_skbs[new]);
583 np->rx_skbs[new] = skb;
584 np->grant_rx_ref[new] = ref;
585 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
586 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
587 np->rx.req_prod_pvt++;
590 static int xennet_get_extras(struct netfront_info *np,
591 struct xen_netif_extra_info *extras,
592 RING_IDX rp)
595 struct xen_netif_extra_info *extra;
596 struct device *dev = &np->netdev->dev;
597 RING_IDX cons = np->rx.rsp_cons;
598 int err = 0;
600 do {
601 struct sk_buff *skb;
602 grant_ref_t ref;
604 if (unlikely(cons + 1 == rp)) {
605 if (net_ratelimit())
606 dev_warn(dev, "Missing extra info\n");
607 err = -EBADR;
608 break;
611 extra = (struct xen_netif_extra_info *)
612 RING_GET_RESPONSE(&np->rx, ++cons);
614 if (unlikely(!extra->type ||
615 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
616 if (net_ratelimit())
617 dev_warn(dev, "Invalid extra type: %d\n",
618 extra->type);
619 err = -EINVAL;
620 } else {
621 memcpy(&extras[extra->type - 1], extra,
622 sizeof(*extra));
625 skb = xennet_get_rx_skb(np, cons);
626 ref = xennet_get_rx_ref(np, cons);
627 xennet_move_rx_slot(np, skb, ref);
628 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
630 np->rx.rsp_cons = cons;
631 return err;
634 static int xennet_get_responses(struct netfront_info *np,
635 struct netfront_rx_info *rinfo, RING_IDX rp,
636 struct sk_buff_head *list)
638 struct xen_netif_rx_response *rx = &rinfo->rx;
639 struct xen_netif_extra_info *extras = rinfo->extras;
640 struct device *dev = &np->netdev->dev;
641 RING_IDX cons = np->rx.rsp_cons;
642 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
643 grant_ref_t ref = xennet_get_rx_ref(np, cons);
644 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
645 int frags = 1;
646 int err = 0;
647 unsigned long ret;
649 if (rx->flags & NETRXF_extra_info) {
650 err = xennet_get_extras(np, extras, rp);
651 cons = np->rx.rsp_cons;
654 for (;;) {
655 if (unlikely(rx->status < 0 ||
656 rx->offset + rx->status > PAGE_SIZE)) {
657 if (net_ratelimit())
658 dev_warn(dev, "rx->offset: %x, size: %u\n",
659 rx->offset, rx->status);
660 xennet_move_rx_slot(np, skb, ref);
661 err = -EINVAL;
662 goto next;
666 * This definitely indicates a bug, either in this driver or in
667 * the backend driver. In future this should flag the bad
668 * situation to the system controller to reboot the backed.
670 if (ref == GRANT_INVALID_REF) {
671 if (net_ratelimit())
672 dev_warn(dev, "Bad rx response id %d.\n",
673 rx->id);
674 err = -EINVAL;
675 goto next;
678 ret = gnttab_end_foreign_access_ref(ref, 0);
679 BUG_ON(!ret);
681 gnttab_release_grant_reference(&np->gref_rx_head, ref);
683 __skb_queue_tail(list, skb);
685 next:
686 if (!(rx->flags & NETRXF_more_data))
687 break;
689 if (cons + frags == rp) {
690 if (net_ratelimit())
691 dev_warn(dev, "Need more frags\n");
692 err = -ENOENT;
693 break;
696 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
697 skb = xennet_get_rx_skb(np, cons + frags);
698 ref = xennet_get_rx_ref(np, cons + frags);
699 frags++;
702 if (unlikely(frags > max)) {
703 if (net_ratelimit())
704 dev_warn(dev, "Too many frags\n");
705 err = -E2BIG;
708 if (unlikely(err))
709 np->rx.rsp_cons = cons + frags;
711 return err;
714 static int xennet_set_skb_gso(struct sk_buff *skb,
715 struct xen_netif_extra_info *gso)
717 if (!gso->u.gso.size) {
718 if (net_ratelimit())
719 printk(KERN_WARNING "GSO size must not be zero.\n");
720 return -EINVAL;
723 /* Currently only TCPv4 S.O. is supported. */
724 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
725 if (net_ratelimit())
726 printk(KERN_WARNING "Bad GSO type %d.\n", gso->u.gso.type);
727 return -EINVAL;
730 skb_shinfo(skb)->gso_size = gso->u.gso.size;
731 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
733 /* Header must be checked, and gso_segs computed. */
734 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
735 skb_shinfo(skb)->gso_segs = 0;
737 return 0;
740 static RING_IDX xennet_fill_frags(struct netfront_info *np,
741 struct sk_buff *skb,
742 struct sk_buff_head *list)
744 struct skb_shared_info *shinfo = skb_shinfo(skb);
745 int nr_frags = shinfo->nr_frags;
746 RING_IDX cons = np->rx.rsp_cons;
747 skb_frag_t *frag = shinfo->frags + nr_frags;
748 struct sk_buff *nskb;
750 while ((nskb = __skb_dequeue(list))) {
751 struct xen_netif_rx_response *rx =
752 RING_GET_RESPONSE(&np->rx, ++cons);
754 frag->page = skb_shinfo(nskb)->frags[0].page;
755 frag->page_offset = rx->offset;
756 frag->size = rx->status;
758 skb->data_len += rx->status;
760 skb_shinfo(nskb)->nr_frags = 0;
761 kfree_skb(nskb);
763 frag++;
764 nr_frags++;
767 shinfo->nr_frags = nr_frags;
768 return cons;
771 static int skb_checksum_setup(struct sk_buff *skb)
773 struct iphdr *iph;
774 unsigned char *th;
775 int err = -EPROTO;
777 if (skb->protocol != htons(ETH_P_IP))
778 goto out;
780 iph = (void *)skb->data;
781 th = skb->data + 4 * iph->ihl;
782 if (th >= skb_tail_pointer(skb))
783 goto out;
785 skb->csum_start = th - skb->head;
786 switch (iph->protocol) {
787 case IPPROTO_TCP:
788 skb->csum_offset = offsetof(struct tcphdr, check);
789 break;
790 case IPPROTO_UDP:
791 skb->csum_offset = offsetof(struct udphdr, check);
792 break;
793 default:
794 if (net_ratelimit())
795 printk(KERN_ERR "Attempting to checksum a non-"
796 "TCP/UDP packet, dropping a protocol"
797 " %d packet", iph->protocol);
798 goto out;
801 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
802 goto out;
804 err = 0;
806 out:
807 return err;
810 static int handle_incoming_queue(struct net_device *dev,
811 struct sk_buff_head *rxq)
813 int packets_dropped = 0;
814 struct sk_buff *skb;
816 while ((skb = __skb_dequeue(rxq)) != NULL) {
817 struct page *page = NETFRONT_SKB_CB(skb)->page;
818 void *vaddr = page_address(page);
819 unsigned offset = NETFRONT_SKB_CB(skb)->offset;
821 memcpy(skb->data, vaddr + offset,
822 skb_headlen(skb));
824 if (page != skb_shinfo(skb)->frags[0].page)
825 __free_page(page);
827 /* Ethernet work: Delayed to here as it peeks the header. */
828 skb->protocol = eth_type_trans(skb, dev);
830 if (skb->ip_summed == CHECKSUM_PARTIAL) {
831 if (skb_checksum_setup(skb)) {
832 kfree_skb(skb);
833 packets_dropped++;
834 dev->stats.rx_errors++;
835 continue;
839 dev->stats.rx_packets++;
840 dev->stats.rx_bytes += skb->len;
842 /* Pass it up. */
843 netif_receive_skb(skb);
844 dev->last_rx = jiffies;
847 return packets_dropped;
850 static int xennet_poll(struct napi_struct *napi, int budget)
852 struct netfront_info *np = container_of(napi, struct netfront_info, napi);
853 struct net_device *dev = np->netdev;
854 struct sk_buff *skb;
855 struct netfront_rx_info rinfo;
856 struct xen_netif_rx_response *rx = &rinfo.rx;
857 struct xen_netif_extra_info *extras = rinfo.extras;
858 RING_IDX i, rp;
859 int work_done;
860 struct sk_buff_head rxq;
861 struct sk_buff_head errq;
862 struct sk_buff_head tmpq;
863 unsigned long flags;
864 unsigned int len;
865 int err;
867 spin_lock(&np->rx_lock);
869 skb_queue_head_init(&rxq);
870 skb_queue_head_init(&errq);
871 skb_queue_head_init(&tmpq);
873 rp = np->rx.sring->rsp_prod;
874 rmb(); /* Ensure we see queued responses up to 'rp'. */
876 i = np->rx.rsp_cons;
877 work_done = 0;
878 while ((i != rp) && (work_done < budget)) {
879 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
880 memset(extras, 0, sizeof(rinfo.extras));
882 err = xennet_get_responses(np, &rinfo, rp, &tmpq);
884 if (unlikely(err)) {
885 err:
886 while ((skb = __skb_dequeue(&tmpq)))
887 __skb_queue_tail(&errq, skb);
888 dev->stats.rx_errors++;
889 i = np->rx.rsp_cons;
890 continue;
893 skb = __skb_dequeue(&tmpq);
895 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
896 struct xen_netif_extra_info *gso;
897 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
899 if (unlikely(xennet_set_skb_gso(skb, gso))) {
900 __skb_queue_head(&tmpq, skb);
901 np->rx.rsp_cons += skb_queue_len(&tmpq);
902 goto err;
906 NETFRONT_SKB_CB(skb)->page = skb_shinfo(skb)->frags[0].page;
907 NETFRONT_SKB_CB(skb)->offset = rx->offset;
909 len = rx->status;
910 if (len > RX_COPY_THRESHOLD)
911 len = RX_COPY_THRESHOLD;
912 skb_put(skb, len);
914 if (rx->status > len) {
915 skb_shinfo(skb)->frags[0].page_offset =
916 rx->offset + len;
917 skb_shinfo(skb)->frags[0].size = rx->status - len;
918 skb->data_len = rx->status - len;
919 } else {
920 skb_shinfo(skb)->frags[0].page = NULL;
921 skb_shinfo(skb)->nr_frags = 0;
924 i = xennet_fill_frags(np, skb, &tmpq);
927 * Truesize approximates the size of true data plus
928 * any supervisor overheads. Adding hypervisor
929 * overheads has been shown to significantly reduce
930 * achievable bandwidth with the default receive
931 * buffer size. It is therefore not wise to account
932 * for it here.
934 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set
935 * to RX_COPY_THRESHOLD + the supervisor
936 * overheads. Here, we add the size of the data pulled
937 * in xennet_fill_frags().
939 * We also adjust for any unused space in the main
940 * data area by subtracting (RX_COPY_THRESHOLD -
941 * len). This is especially important with drivers
942 * which split incoming packets into header and data,
943 * using only 66 bytes of the main data area (see the
944 * e1000 driver for example.) On such systems,
945 * without this last adjustement, our achievable
946 * receive throughout using the standard receive
947 * buffer size was cut by 25%(!!!).
949 skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
950 skb->len += skb->data_len;
952 if (rx->flags & NETRXF_csum_blank)
953 skb->ip_summed = CHECKSUM_PARTIAL;
954 else if (rx->flags & NETRXF_data_validated)
955 skb->ip_summed = CHECKSUM_UNNECESSARY;
957 __skb_queue_tail(&rxq, skb);
959 np->rx.rsp_cons = ++i;
960 work_done++;
963 __skb_queue_purge(&errq);
965 work_done -= handle_incoming_queue(dev, &rxq);
967 /* If we get a callback with very few responses, reduce fill target. */
968 /* NB. Note exponential increase, linear decrease. */
969 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
970 ((3*np->rx_target) / 4)) &&
971 (--np->rx_target < np->rx_min_target))
972 np->rx_target = np->rx_min_target;
974 xennet_alloc_rx_buffers(dev);
976 if (work_done < budget) {
977 int more_to_do = 0;
979 local_irq_save(flags);
981 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
982 if (!more_to_do)
983 __netif_rx_complete(dev, napi);
985 local_irq_restore(flags);
988 spin_unlock(&np->rx_lock);
990 return work_done;
993 static int xennet_change_mtu(struct net_device *dev, int mtu)
995 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
997 if (mtu > max)
998 return -EINVAL;
999 dev->mtu = mtu;
1000 return 0;
1003 static void xennet_release_tx_bufs(struct netfront_info *np)
1005 struct sk_buff *skb;
1006 int i;
1008 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1009 /* Skip over entries which are actually freelist references */
1010 if (skb_entry_is_link(&np->tx_skbs[i]))
1011 continue;
1013 skb = np->tx_skbs[i].skb;
1014 gnttab_end_foreign_access_ref(np->grant_tx_ref[i],
1015 GNTMAP_readonly);
1016 gnttab_release_grant_reference(&np->gref_tx_head,
1017 np->grant_tx_ref[i]);
1018 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1019 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, i);
1020 dev_kfree_skb_irq(skb);
1024 static void xennet_release_rx_bufs(struct netfront_info *np)
1026 struct mmu_update *mmu = np->rx_mmu;
1027 struct multicall_entry *mcl = np->rx_mcl;
1028 struct sk_buff_head free_list;
1029 struct sk_buff *skb;
1030 unsigned long mfn;
1031 int xfer = 0, noxfer = 0, unused = 0;
1032 int id, ref;
1034 dev_warn(&np->netdev->dev, "%s: fix me for copying receiver.\n",
1035 __func__);
1036 return;
1038 skb_queue_head_init(&free_list);
1040 spin_lock_bh(&np->rx_lock);
1042 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1043 ref = np->grant_rx_ref[id];
1044 if (ref == GRANT_INVALID_REF) {
1045 unused++;
1046 continue;
1049 skb = np->rx_skbs[id];
1050 mfn = gnttab_end_foreign_transfer_ref(ref);
1051 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1052 np->grant_rx_ref[id] = GRANT_INVALID_REF;
1054 if (0 == mfn) {
1055 skb_shinfo(skb)->nr_frags = 0;
1056 dev_kfree_skb(skb);
1057 noxfer++;
1058 continue;
1061 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1062 /* Remap the page. */
1063 struct page *page = skb_shinfo(skb)->frags[0].page;
1064 unsigned long pfn = page_to_pfn(page);
1065 void *vaddr = page_address(page);
1067 MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
1068 mfn_pte(mfn, PAGE_KERNEL),
1070 mcl++;
1071 mmu->ptr = ((u64)mfn << PAGE_SHIFT)
1072 | MMU_MACHPHYS_UPDATE;
1073 mmu->val = pfn;
1074 mmu++;
1076 set_phys_to_machine(pfn, mfn);
1078 __skb_queue_tail(&free_list, skb);
1079 xfer++;
1082 dev_info(&np->netdev->dev, "%s: %d xfer, %d noxfer, %d unused\n",
1083 __func__, xfer, noxfer, unused);
1085 if (xfer) {
1086 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1087 /* Do all the remapping work and M2P updates. */
1088 MULTI_mmu_update(mcl, np->rx_mmu, mmu - np->rx_mmu,
1089 NULL, DOMID_SELF);
1090 mcl++;
1091 HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
1095 __skb_queue_purge(&free_list);
1097 spin_unlock_bh(&np->rx_lock);
1100 static void xennet_uninit(struct net_device *dev)
1102 struct netfront_info *np = netdev_priv(dev);
1103 xennet_release_tx_bufs(np);
1104 xennet_release_rx_bufs(np);
1105 gnttab_free_grant_references(np->gref_tx_head);
1106 gnttab_free_grant_references(np->gref_rx_head);
1109 static struct net_device * __devinit xennet_create_dev(struct xenbus_device *dev)
1111 int i, err;
1112 struct net_device *netdev;
1113 struct netfront_info *np;
1115 netdev = alloc_etherdev(sizeof(struct netfront_info));
1116 if (!netdev) {
1117 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1118 __func__);
1119 return ERR_PTR(-ENOMEM);
1122 np = netdev_priv(netdev);
1123 np->xbdev = dev;
1125 spin_lock_init(&np->tx_lock);
1126 spin_lock_init(&np->rx_lock);
1128 skb_queue_head_init(&np->rx_batch);
1129 np->rx_target = RX_DFL_MIN_TARGET;
1130 np->rx_min_target = RX_DFL_MIN_TARGET;
1131 np->rx_max_target = RX_MAX_TARGET;
1133 init_timer(&np->rx_refill_timer);
1134 np->rx_refill_timer.data = (unsigned long)netdev;
1135 np->rx_refill_timer.function = rx_refill_timeout;
1137 /* Initialise tx_skbs as a free chain containing every entry. */
1138 np->tx_skb_freelist = 0;
1139 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1140 skb_entry_set_link(&np->tx_skbs[i], i+1);
1141 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1144 /* Clear out rx_skbs */
1145 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1146 np->rx_skbs[i] = NULL;
1147 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1150 /* A grant for every tx ring slot */
1151 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1152 &np->gref_tx_head) < 0) {
1153 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1154 err = -ENOMEM;
1155 goto exit;
1157 /* A grant for every rx ring slot */
1158 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1159 &np->gref_rx_head) < 0) {
1160 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1161 err = -ENOMEM;
1162 goto exit_free_tx;
1165 netdev->open = xennet_open;
1166 netdev->hard_start_xmit = xennet_start_xmit;
1167 netdev->stop = xennet_close;
1168 netif_napi_add(netdev, &np->napi, xennet_poll, 64);
1169 netdev->uninit = xennet_uninit;
1170 netdev->change_mtu = xennet_change_mtu;
1171 netdev->features = NETIF_F_IP_CSUM;
1173 SET_ETHTOOL_OPS(netdev, &xennet_ethtool_ops);
1174 SET_NETDEV_DEV(netdev, &dev->dev);
1176 np->netdev = netdev;
1178 netif_carrier_off(netdev);
1180 return netdev;
1182 exit_free_tx:
1183 gnttab_free_grant_references(np->gref_tx_head);
1184 exit:
1185 free_netdev(netdev);
1186 return ERR_PTR(err);
1190 * Entry point to this code when a new device is created. Allocate the basic
1191 * structures and the ring buffers for communication with the backend, and
1192 * inform the backend of the appropriate details for those.
1194 static int __devinit netfront_probe(struct xenbus_device *dev,
1195 const struct xenbus_device_id *id)
1197 int err;
1198 struct net_device *netdev;
1199 struct netfront_info *info;
1201 netdev = xennet_create_dev(dev);
1202 if (IS_ERR(netdev)) {
1203 err = PTR_ERR(netdev);
1204 xenbus_dev_fatal(dev, err, "creating netdev");
1205 return err;
1208 info = netdev_priv(netdev);
1209 dev->dev.driver_data = info;
1211 err = register_netdev(info->netdev);
1212 if (err) {
1213 printk(KERN_WARNING "%s: register_netdev err=%d\n",
1214 __func__, err);
1215 goto fail;
1218 err = xennet_sysfs_addif(info->netdev);
1219 if (err) {
1220 unregister_netdev(info->netdev);
1221 printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
1222 __func__, err);
1223 goto fail;
1226 return 0;
1228 fail:
1229 free_netdev(netdev);
1230 dev->dev.driver_data = NULL;
1231 return err;
1234 static void xennet_end_access(int ref, void *page)
1236 /* This frees the page as a side-effect */
1237 if (ref != GRANT_INVALID_REF)
1238 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1241 static void xennet_disconnect_backend(struct netfront_info *info)
1243 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1244 spin_lock_bh(&info->rx_lock);
1245 spin_lock_irq(&info->tx_lock);
1246 netif_carrier_off(info->netdev);
1247 spin_unlock_irq(&info->tx_lock);
1248 spin_unlock_bh(&info->rx_lock);
1250 if (info->netdev->irq)
1251 unbind_from_irqhandler(info->netdev->irq, info->netdev);
1252 info->evtchn = info->netdev->irq = 0;
1254 /* End access and free the pages */
1255 xennet_end_access(info->tx_ring_ref, info->tx.sring);
1256 xennet_end_access(info->rx_ring_ref, info->rx.sring);
1258 info->tx_ring_ref = GRANT_INVALID_REF;
1259 info->rx_ring_ref = GRANT_INVALID_REF;
1260 info->tx.sring = NULL;
1261 info->rx.sring = NULL;
1265 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1266 * driver restart. We tear down our netif structure and recreate it, but
1267 * leave the device-layer structures intact so that this is transparent to the
1268 * rest of the kernel.
1270 static int netfront_resume(struct xenbus_device *dev)
1272 struct netfront_info *info = dev->dev.driver_data;
1274 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1276 xennet_disconnect_backend(info);
1277 return 0;
1280 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1282 char *s, *e, *macstr;
1283 int i;
1285 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1286 if (IS_ERR(macstr))
1287 return PTR_ERR(macstr);
1289 for (i = 0; i < ETH_ALEN; i++) {
1290 mac[i] = simple_strtoul(s, &e, 16);
1291 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1292 kfree(macstr);
1293 return -ENOENT;
1295 s = e+1;
1298 kfree(macstr);
1299 return 0;
1302 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1304 struct net_device *dev = dev_id;
1305 struct netfront_info *np = netdev_priv(dev);
1306 unsigned long flags;
1308 spin_lock_irqsave(&np->tx_lock, flags);
1310 if (likely(netif_carrier_ok(dev))) {
1311 xennet_tx_buf_gc(dev);
1312 /* Under tx_lock: protects access to rx shared-ring indexes. */
1313 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
1314 netif_rx_schedule(dev, &np->napi);
1317 spin_unlock_irqrestore(&np->tx_lock, flags);
1319 return IRQ_HANDLED;
1322 static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)
1324 struct xen_netif_tx_sring *txs;
1325 struct xen_netif_rx_sring *rxs;
1326 int err;
1327 struct net_device *netdev = info->netdev;
1329 info->tx_ring_ref = GRANT_INVALID_REF;
1330 info->rx_ring_ref = GRANT_INVALID_REF;
1331 info->rx.sring = NULL;
1332 info->tx.sring = NULL;
1333 netdev->irq = 0;
1335 err = xen_net_read_mac(dev, netdev->dev_addr);
1336 if (err) {
1337 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1338 goto fail;
1341 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1342 if (!txs) {
1343 err = -ENOMEM;
1344 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1345 goto fail;
1347 SHARED_RING_INIT(txs);
1348 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
1350 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
1351 if (err < 0) {
1352 free_page((unsigned long)txs);
1353 goto fail;
1356 info->tx_ring_ref = err;
1357 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1358 if (!rxs) {
1359 err = -ENOMEM;
1360 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1361 goto fail;
1363 SHARED_RING_INIT(rxs);
1364 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
1366 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
1367 if (err < 0) {
1368 free_page((unsigned long)rxs);
1369 goto fail;
1371 info->rx_ring_ref = err;
1373 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1374 if (err)
1375 goto fail;
1377 err = bind_evtchn_to_irqhandler(info->evtchn, xennet_interrupt,
1378 IRQF_SAMPLE_RANDOM, netdev->name,
1379 netdev);
1380 if (err < 0)
1381 goto fail;
1382 netdev->irq = err;
1383 return 0;
1385 fail:
1386 return err;
1389 /* Common code used when first setting up, and when resuming. */
1390 static int talk_to_backend(struct xenbus_device *dev,
1391 struct netfront_info *info)
1393 const char *message;
1394 struct xenbus_transaction xbt;
1395 int err;
1397 /* Create shared ring, alloc event channel. */
1398 err = setup_netfront(dev, info);
1399 if (err)
1400 goto out;
1402 again:
1403 err = xenbus_transaction_start(&xbt);
1404 if (err) {
1405 xenbus_dev_fatal(dev, err, "starting transaction");
1406 goto destroy_ring;
1409 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
1410 info->tx_ring_ref);
1411 if (err) {
1412 message = "writing tx ring-ref";
1413 goto abort_transaction;
1415 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
1416 info->rx_ring_ref);
1417 if (err) {
1418 message = "writing rx ring-ref";
1419 goto abort_transaction;
1421 err = xenbus_printf(xbt, dev->nodename,
1422 "event-channel", "%u", info->evtchn);
1423 if (err) {
1424 message = "writing event-channel";
1425 goto abort_transaction;
1428 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1430 if (err) {
1431 message = "writing request-rx-copy";
1432 goto abort_transaction;
1435 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1436 if (err) {
1437 message = "writing feature-rx-notify";
1438 goto abort_transaction;
1441 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1442 if (err) {
1443 message = "writing feature-sg";
1444 goto abort_transaction;
1447 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1448 if (err) {
1449 message = "writing feature-gso-tcpv4";
1450 goto abort_transaction;
1453 err = xenbus_transaction_end(xbt, 0);
1454 if (err) {
1455 if (err == -EAGAIN)
1456 goto again;
1457 xenbus_dev_fatal(dev, err, "completing transaction");
1458 goto destroy_ring;
1461 return 0;
1463 abort_transaction:
1464 xenbus_transaction_end(xbt, 1);
1465 xenbus_dev_fatal(dev, err, "%s", message);
1466 destroy_ring:
1467 xennet_disconnect_backend(info);
1468 out:
1469 return err;
1472 static int xennet_set_sg(struct net_device *dev, u32 data)
1474 if (data) {
1475 struct netfront_info *np = netdev_priv(dev);
1476 int val;
1478 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1479 "%d", &val) < 0)
1480 val = 0;
1481 if (!val)
1482 return -ENOSYS;
1483 } else if (dev->mtu > ETH_DATA_LEN)
1484 dev->mtu = ETH_DATA_LEN;
1486 return ethtool_op_set_sg(dev, data);
1489 static int xennet_set_tso(struct net_device *dev, u32 data)
1491 if (data) {
1492 struct netfront_info *np = netdev_priv(dev);
1493 int val;
1495 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1496 "feature-gso-tcpv4", "%d", &val) < 0)
1497 val = 0;
1498 if (!val)
1499 return -ENOSYS;
1502 return ethtool_op_set_tso(dev, data);
1505 static void xennet_set_features(struct net_device *dev)
1507 /* Turn off all GSO bits except ROBUST. */
1508 dev->features &= (1 << NETIF_F_GSO_SHIFT) - 1;
1509 dev->features |= NETIF_F_GSO_ROBUST;
1510 xennet_set_sg(dev, 0);
1512 /* We need checksum offload to enable scatter/gather and TSO. */
1513 if (!(dev->features & NETIF_F_IP_CSUM))
1514 return;
1516 if (!xennet_set_sg(dev, 1))
1517 xennet_set_tso(dev, 1);
1520 static int xennet_connect(struct net_device *dev)
1522 struct netfront_info *np = netdev_priv(dev);
1523 int i, requeue_idx, err;
1524 struct sk_buff *skb;
1525 grant_ref_t ref;
1526 struct xen_netif_rx_request *req;
1527 unsigned int feature_rx_copy;
1529 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1530 "feature-rx-copy", "%u", &feature_rx_copy);
1531 if (err != 1)
1532 feature_rx_copy = 0;
1534 if (!feature_rx_copy) {
1535 dev_info(&dev->dev,
1536 "backend does not support copying receive path\n");
1537 return -ENODEV;
1540 err = talk_to_backend(np->xbdev, np);
1541 if (err)
1542 return err;
1544 xennet_set_features(dev);
1546 spin_lock_bh(&np->rx_lock);
1547 spin_lock_irq(&np->tx_lock);
1549 /* Step 1: Discard all pending TX packet fragments. */
1550 xennet_release_tx_bufs(np);
1552 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1553 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1554 if (!np->rx_skbs[i])
1555 continue;
1557 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1558 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1559 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1561 gnttab_grant_foreign_access_ref(
1562 ref, np->xbdev->otherend_id,
1563 pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
1564 frags->page)),
1566 req->gref = ref;
1567 req->id = requeue_idx;
1569 requeue_idx++;
1572 np->rx.req_prod_pvt = requeue_idx;
1575 * Step 3: All public and private state should now be sane. Get
1576 * ready to start sending and receiving packets and give the driver
1577 * domain a kick because we've probably just requeued some
1578 * packets.
1580 netif_carrier_on(np->netdev);
1581 notify_remote_via_irq(np->netdev->irq);
1582 xennet_tx_buf_gc(dev);
1583 xennet_alloc_rx_buffers(dev);
1585 spin_unlock_irq(&np->tx_lock);
1586 spin_unlock_bh(&np->rx_lock);
1588 return 0;
1592 * Callback received when the backend's state changes.
1594 static void backend_changed(struct xenbus_device *dev,
1595 enum xenbus_state backend_state)
1597 struct netfront_info *np = dev->dev.driver_data;
1598 struct net_device *netdev = np->netdev;
1600 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
1602 switch (backend_state) {
1603 case XenbusStateInitialising:
1604 case XenbusStateInitialised:
1605 case XenbusStateConnected:
1606 case XenbusStateUnknown:
1607 case XenbusStateClosed:
1608 break;
1610 case XenbusStateInitWait:
1611 if (dev->state != XenbusStateInitialising)
1612 break;
1613 if (xennet_connect(netdev) != 0)
1614 break;
1615 xenbus_switch_state(dev, XenbusStateConnected);
1616 break;
1618 case XenbusStateClosing:
1619 xenbus_frontend_closed(dev);
1620 break;
1624 static struct ethtool_ops xennet_ethtool_ops =
1626 .set_tx_csum = ethtool_op_set_tx_csum,
1627 .set_sg = xennet_set_sg,
1628 .set_tso = xennet_set_tso,
1629 .get_link = ethtool_op_get_link,
1632 #ifdef CONFIG_SYSFS
1633 static ssize_t show_rxbuf_min(struct device *dev,
1634 struct device_attribute *attr, char *buf)
1636 struct net_device *netdev = to_net_dev(dev);
1637 struct netfront_info *info = netdev_priv(netdev);
1639 return sprintf(buf, "%u\n", info->rx_min_target);
1642 static ssize_t store_rxbuf_min(struct device *dev,
1643 struct device_attribute *attr,
1644 const char *buf, size_t len)
1646 struct net_device *netdev = to_net_dev(dev);
1647 struct netfront_info *np = netdev_priv(netdev);
1648 char *endp;
1649 unsigned long target;
1651 if (!capable(CAP_NET_ADMIN))
1652 return -EPERM;
1654 target = simple_strtoul(buf, &endp, 0);
1655 if (endp == buf)
1656 return -EBADMSG;
1658 if (target < RX_MIN_TARGET)
1659 target = RX_MIN_TARGET;
1660 if (target > RX_MAX_TARGET)
1661 target = RX_MAX_TARGET;
1663 spin_lock_bh(&np->rx_lock);
1664 if (target > np->rx_max_target)
1665 np->rx_max_target = target;
1666 np->rx_min_target = target;
1667 if (target > np->rx_target)
1668 np->rx_target = target;
1670 xennet_alloc_rx_buffers(netdev);
1672 spin_unlock_bh(&np->rx_lock);
1673 return len;
1676 static ssize_t show_rxbuf_max(struct device *dev,
1677 struct device_attribute *attr, char *buf)
1679 struct net_device *netdev = to_net_dev(dev);
1680 struct netfront_info *info = netdev_priv(netdev);
1682 return sprintf(buf, "%u\n", info->rx_max_target);
1685 static ssize_t store_rxbuf_max(struct device *dev,
1686 struct device_attribute *attr,
1687 const char *buf, size_t len)
1689 struct net_device *netdev = to_net_dev(dev);
1690 struct netfront_info *np = netdev_priv(netdev);
1691 char *endp;
1692 unsigned long target;
1694 if (!capable(CAP_NET_ADMIN))
1695 return -EPERM;
1697 target = simple_strtoul(buf, &endp, 0);
1698 if (endp == buf)
1699 return -EBADMSG;
1701 if (target < RX_MIN_TARGET)
1702 target = RX_MIN_TARGET;
1703 if (target > RX_MAX_TARGET)
1704 target = RX_MAX_TARGET;
1706 spin_lock_bh(&np->rx_lock);
1707 if (target < np->rx_min_target)
1708 np->rx_min_target = target;
1709 np->rx_max_target = target;
1710 if (target < np->rx_target)
1711 np->rx_target = target;
1713 xennet_alloc_rx_buffers(netdev);
1715 spin_unlock_bh(&np->rx_lock);
1716 return len;
1719 static ssize_t show_rxbuf_cur(struct device *dev,
1720 struct device_attribute *attr, char *buf)
1722 struct net_device *netdev = to_net_dev(dev);
1723 struct netfront_info *info = netdev_priv(netdev);
1725 return sprintf(buf, "%u\n", info->rx_target);
1728 static struct device_attribute xennet_attrs[] = {
1729 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1730 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1731 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1734 static int xennet_sysfs_addif(struct net_device *netdev)
1736 int i;
1737 int err;
1739 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1740 err = device_create_file(&netdev->dev,
1741 &xennet_attrs[i]);
1742 if (err)
1743 goto fail;
1745 return 0;
1747 fail:
1748 while (--i >= 0)
1749 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1750 return err;
1753 static void xennet_sysfs_delif(struct net_device *netdev)
1755 int i;
1757 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
1758 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1761 #endif /* CONFIG_SYSFS */
1763 static struct xenbus_device_id netfront_ids[] = {
1764 { "vif" },
1765 { "" }
1769 static int __devexit xennet_remove(struct xenbus_device *dev)
1771 struct netfront_info *info = dev->dev.driver_data;
1773 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1775 unregister_netdev(info->netdev);
1777 xennet_disconnect_backend(info);
1779 del_timer_sync(&info->rx_refill_timer);
1781 xennet_sysfs_delif(info->netdev);
1783 free_netdev(info->netdev);
1785 return 0;
1788 static struct xenbus_driver netfront_driver = {
1789 .name = "vif",
1790 .owner = THIS_MODULE,
1791 .ids = netfront_ids,
1792 .probe = netfront_probe,
1793 .remove = __devexit_p(xennet_remove),
1794 .resume = netfront_resume,
1795 .otherend_changed = backend_changed,
1798 static int __init netif_init(void)
1800 if (!xen_domain())
1801 return -ENODEV;
1803 if (xen_initial_domain())
1804 return 0;
1806 printk(KERN_INFO "Initialising Xen virtual ethernet driver.\n");
1808 return xenbus_register_frontend(&netfront_driver);
1810 module_init(netif_init);
1813 static void __exit netif_exit(void)
1815 if (xen_initial_domain())
1816 return;
1818 xenbus_unregister_driver(&netfront_driver);
1820 module_exit(netif_exit);
1822 MODULE_DESCRIPTION("Xen virtual network device frontend");
1823 MODULE_LICENSE("GPL");
1824 MODULE_ALIAS("xen:vif");
1825 MODULE_ALIAS("xennet");