rtl8180: use cached queue mapping for skb in rtl8180_tx
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / xen-netfront.c
bloba869b45d3d37b2f38a986c4dfb0146d10be4416d
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/xen.h>
46 #include <xen/xenbus.h>
47 #include <xen/events.h>
48 #include <xen/page.h>
49 #include <xen/grant_table.h>
51 #include <xen/interface/io/netif.h>
52 #include <xen/interface/memory.h>
53 #include <xen/interface/grant_table.h>
55 static const struct ethtool_ops xennet_ethtool_ops;
57 struct netfront_cb {
58 struct page *page;
59 unsigned offset;
62 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
64 #define RX_COPY_THRESHOLD 256
66 #define GRANT_INVALID_REF 0
68 #define NET_TX_RING_SIZE __RING_SIZE((struct xen_netif_tx_sring *)0, PAGE_SIZE)
69 #define NET_RX_RING_SIZE __RING_SIZE((struct xen_netif_rx_sring *)0, PAGE_SIZE)
70 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
72 struct netfront_info {
73 struct list_head list;
74 struct net_device *netdev;
76 struct napi_struct napi;
78 unsigned int evtchn;
79 struct xenbus_device *xbdev;
81 spinlock_t tx_lock;
82 struct xen_netif_tx_front_ring tx;
83 int tx_ring_ref;
86 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
87 * are linked from tx_skb_freelist through skb_entry.link.
89 * NB. Freelist index entries are always going to be less than
90 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
91 * greater than PAGE_OFFSET: we use this property to distinguish
92 * them.
94 union skb_entry {
95 struct sk_buff *skb;
96 unsigned long link;
97 } tx_skbs[NET_TX_RING_SIZE];
98 grant_ref_t gref_tx_head;
99 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
100 unsigned tx_skb_freelist;
102 spinlock_t rx_lock ____cacheline_aligned_in_smp;
103 struct xen_netif_rx_front_ring rx;
104 int rx_ring_ref;
106 /* Receive-ring batched refills. */
107 #define RX_MIN_TARGET 8
108 #define RX_DFL_MIN_TARGET 64
109 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
110 unsigned rx_min_target, rx_max_target, rx_target;
111 struct sk_buff_head rx_batch;
113 struct timer_list rx_refill_timer;
115 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
116 grant_ref_t gref_rx_head;
117 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
119 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
120 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
121 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
124 struct netfront_rx_info {
125 struct xen_netif_rx_response rx;
126 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
129 static void skb_entry_set_link(union skb_entry *list, unsigned short id)
131 list->link = id;
134 static int skb_entry_is_link(const union skb_entry *list)
136 BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
137 return ((unsigned long)list->skb < PAGE_OFFSET);
141 * Access macros for acquiring freeing slots in tx_skbs[].
144 static void add_id_to_freelist(unsigned *head, union skb_entry *list,
145 unsigned short id)
147 skb_entry_set_link(&list[id], *head);
148 *head = id;
151 static unsigned short get_id_from_freelist(unsigned *head,
152 union skb_entry *list)
154 unsigned int id = *head;
155 *head = list[id].link;
156 return id;
159 static int xennet_rxidx(RING_IDX idx)
161 return idx & (NET_RX_RING_SIZE - 1);
164 static struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
165 RING_IDX ri)
167 int i = xennet_rxidx(ri);
168 struct sk_buff *skb = np->rx_skbs[i];
169 np->rx_skbs[i] = NULL;
170 return skb;
173 static grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
174 RING_IDX ri)
176 int i = xennet_rxidx(ri);
177 grant_ref_t ref = np->grant_rx_ref[i];
178 np->grant_rx_ref[i] = GRANT_INVALID_REF;
179 return ref;
182 #ifdef CONFIG_SYSFS
183 static int xennet_sysfs_addif(struct net_device *netdev);
184 static void xennet_sysfs_delif(struct net_device *netdev);
185 #else /* !CONFIG_SYSFS */
186 #define xennet_sysfs_addif(dev) (0)
187 #define xennet_sysfs_delif(dev) do { } while (0)
188 #endif
190 static int xennet_can_sg(struct net_device *dev)
192 return dev->features & NETIF_F_SG;
196 static void rx_refill_timeout(unsigned long data)
198 struct net_device *dev = (struct net_device *)data;
199 struct netfront_info *np = netdev_priv(dev);
200 napi_schedule(&np->napi);
203 static int netfront_tx_slot_available(struct netfront_info *np)
205 return ((np->tx.req_prod_pvt - np->tx.rsp_cons) <
206 (TX_MAX_TARGET - MAX_SKB_FRAGS - 2));
209 static void xennet_maybe_wake_tx(struct net_device *dev)
211 struct netfront_info *np = netdev_priv(dev);
213 if (unlikely(netif_queue_stopped(dev)) &&
214 netfront_tx_slot_available(np) &&
215 likely(netif_running(dev)))
216 netif_wake_queue(dev);
219 static void xennet_alloc_rx_buffers(struct net_device *dev)
221 unsigned short id;
222 struct netfront_info *np = netdev_priv(dev);
223 struct sk_buff *skb;
224 struct page *page;
225 int i, batch_target, notify;
226 RING_IDX req_prod = np->rx.req_prod_pvt;
227 grant_ref_t ref;
228 unsigned long pfn;
229 void *vaddr;
230 struct xen_netif_rx_request *req;
232 if (unlikely(!netif_carrier_ok(dev)))
233 return;
236 * Allocate skbuffs greedily, even though we batch updates to the
237 * receive ring. This creates a less bursty demand on the memory
238 * allocator, so should reduce the chance of failed allocation requests
239 * both for ourself and for other kernel subsystems.
241 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
242 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
243 skb = __netdev_alloc_skb(dev, RX_COPY_THRESHOLD + NET_IP_ALIGN,
244 GFP_ATOMIC | __GFP_NOWARN);
245 if (unlikely(!skb))
246 goto no_skb;
248 /* Align ip header to a 16 bytes boundary */
249 skb_reserve(skb, NET_IP_ALIGN);
251 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
252 if (!page) {
253 kfree_skb(skb);
254 no_skb:
255 /* Any skbuffs queued for refill? Force them out. */
256 if (i != 0)
257 goto refill;
258 /* Could not allocate any skbuffs. Try again later. */
259 mod_timer(&np->rx_refill_timer,
260 jiffies + (HZ/10));
261 break;
264 skb_shinfo(skb)->frags[0].page = page;
265 skb_shinfo(skb)->nr_frags = 1;
266 __skb_queue_tail(&np->rx_batch, skb);
269 /* Is the batch large enough to be worthwhile? */
270 if (i < (np->rx_target/2)) {
271 if (req_prod > np->rx.sring->req_prod)
272 goto push;
273 return;
276 /* Adjust our fill target if we risked running out of buffers. */
277 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
278 ((np->rx_target *= 2) > np->rx_max_target))
279 np->rx_target = np->rx_max_target;
281 refill:
282 for (i = 0; ; i++) {
283 skb = __skb_dequeue(&np->rx_batch);
284 if (skb == NULL)
285 break;
287 skb->dev = dev;
289 id = xennet_rxidx(req_prod + i);
291 BUG_ON(np->rx_skbs[id]);
292 np->rx_skbs[id] = skb;
294 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
295 BUG_ON((signed short)ref < 0);
296 np->grant_rx_ref[id] = ref;
298 pfn = page_to_pfn(skb_shinfo(skb)->frags[0].page);
299 vaddr = page_address(skb_shinfo(skb)->frags[0].page);
301 req = RING_GET_REQUEST(&np->rx, req_prod + i);
302 gnttab_grant_foreign_access_ref(ref,
303 np->xbdev->otherend_id,
304 pfn_to_mfn(pfn),
307 req->id = id;
308 req->gref = ref;
311 wmb(); /* barrier so backend seens requests */
313 /* Above is a suitable barrier to ensure backend will see requests. */
314 np->rx.req_prod_pvt = req_prod + i;
315 push:
316 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
317 if (notify)
318 notify_remote_via_irq(np->netdev->irq);
321 static int xennet_open(struct net_device *dev)
323 struct netfront_info *np = netdev_priv(dev);
325 napi_enable(&np->napi);
327 spin_lock_bh(&np->rx_lock);
328 if (netif_carrier_ok(dev)) {
329 xennet_alloc_rx_buffers(dev);
330 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
331 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
332 napi_schedule(&np->napi);
334 spin_unlock_bh(&np->rx_lock);
336 netif_start_queue(dev);
338 return 0;
341 static void xennet_tx_buf_gc(struct net_device *dev)
343 RING_IDX cons, prod;
344 unsigned short id;
345 struct netfront_info *np = netdev_priv(dev);
346 struct sk_buff *skb;
348 BUG_ON(!netif_carrier_ok(dev));
350 do {
351 prod = np->tx.sring->rsp_prod;
352 rmb(); /* Ensure we see responses up to 'rp'. */
354 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
355 struct xen_netif_tx_response *txrsp;
357 txrsp = RING_GET_RESPONSE(&np->tx, cons);
358 if (txrsp->status == NETIF_RSP_NULL)
359 continue;
361 id = txrsp->id;
362 skb = np->tx_skbs[id].skb;
363 if (unlikely(gnttab_query_foreign_access(
364 np->grant_tx_ref[id]) != 0)) {
365 printk(KERN_ALERT "xennet_tx_buf_gc: warning "
366 "-- grant still in use by backend "
367 "domain.\n");
368 BUG();
370 gnttab_end_foreign_access_ref(
371 np->grant_tx_ref[id], GNTMAP_readonly);
372 gnttab_release_grant_reference(
373 &np->gref_tx_head, np->grant_tx_ref[id]);
374 np->grant_tx_ref[id] = GRANT_INVALID_REF;
375 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, id);
376 dev_kfree_skb_irq(skb);
379 np->tx.rsp_cons = prod;
382 * Set a new event, then check for race with update of tx_cons.
383 * Note that it is essential to schedule a callback, no matter
384 * how few buffers are pending. Even if there is space in the
385 * transmit ring, higher layers may be blocked because too much
386 * data is outstanding: in such cases notification from Xen is
387 * likely to be the only kick that we'll get.
389 np->tx.sring->rsp_event =
390 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
391 mb(); /* update shared area */
392 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
394 xennet_maybe_wake_tx(dev);
397 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
398 struct xen_netif_tx_request *tx)
400 struct netfront_info *np = netdev_priv(dev);
401 char *data = skb->data;
402 unsigned long mfn;
403 RING_IDX prod = np->tx.req_prod_pvt;
404 int frags = skb_shinfo(skb)->nr_frags;
405 unsigned int offset = offset_in_page(data);
406 unsigned int len = skb_headlen(skb);
407 unsigned int id;
408 grant_ref_t ref;
409 int i;
411 /* While the header overlaps a page boundary (including being
412 larger than a page), split it it into page-sized chunks. */
413 while (len > PAGE_SIZE - offset) {
414 tx->size = PAGE_SIZE - offset;
415 tx->flags |= NETTXF_more_data;
416 len -= tx->size;
417 data += tx->size;
418 offset = 0;
420 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
421 np->tx_skbs[id].skb = skb_get(skb);
422 tx = RING_GET_REQUEST(&np->tx, prod++);
423 tx->id = id;
424 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
425 BUG_ON((signed short)ref < 0);
427 mfn = virt_to_mfn(data);
428 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
429 mfn, GNTMAP_readonly);
431 tx->gref = np->grant_tx_ref[id] = ref;
432 tx->offset = offset;
433 tx->size = len;
434 tx->flags = 0;
437 /* Grant backend access to each skb fragment page. */
438 for (i = 0; i < frags; i++) {
439 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
441 tx->flags |= NETTXF_more_data;
443 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
444 np->tx_skbs[id].skb = skb_get(skb);
445 tx = RING_GET_REQUEST(&np->tx, prod++);
446 tx->id = id;
447 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
448 BUG_ON((signed short)ref < 0);
450 mfn = pfn_to_mfn(page_to_pfn(frag->page));
451 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
452 mfn, GNTMAP_readonly);
454 tx->gref = np->grant_tx_ref[id] = ref;
455 tx->offset = frag->page_offset;
456 tx->size = frag->size;
457 tx->flags = 0;
460 np->tx.req_prod_pvt = prod;
463 static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
465 unsigned short id;
466 struct netfront_info *np = netdev_priv(dev);
467 struct xen_netif_tx_request *tx;
468 struct xen_netif_extra_info *extra;
469 char *data = skb->data;
470 RING_IDX i;
471 grant_ref_t ref;
472 unsigned long mfn;
473 int notify;
474 int frags = skb_shinfo(skb)->nr_frags;
475 unsigned int offset = offset_in_page(data);
476 unsigned int len = skb_headlen(skb);
478 frags += DIV_ROUND_UP(offset + len, PAGE_SIZE);
479 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
480 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
481 frags);
482 dump_stack();
483 goto drop;
486 spin_lock_irq(&np->tx_lock);
488 if (unlikely(!netif_carrier_ok(dev) ||
489 (frags > 1 && !xennet_can_sg(dev)) ||
490 netif_needs_gso(dev, skb))) {
491 spin_unlock_irq(&np->tx_lock);
492 goto drop;
495 i = np->tx.req_prod_pvt;
497 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
498 np->tx_skbs[id].skb = skb;
500 tx = RING_GET_REQUEST(&np->tx, i);
502 tx->id = id;
503 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
504 BUG_ON((signed short)ref < 0);
505 mfn = virt_to_mfn(data);
506 gnttab_grant_foreign_access_ref(
507 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
508 tx->gref = np->grant_tx_ref[id] = ref;
509 tx->offset = offset;
510 tx->size = len;
511 extra = NULL;
513 tx->flags = 0;
514 if (skb->ip_summed == CHECKSUM_PARTIAL)
515 /* local packet? */
516 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
517 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
518 /* remote but checksummed. */
519 tx->flags |= NETTXF_data_validated;
521 if (skb_shinfo(skb)->gso_size) {
522 struct xen_netif_extra_info *gso;
524 gso = (struct xen_netif_extra_info *)
525 RING_GET_REQUEST(&np->tx, ++i);
527 if (extra)
528 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
529 else
530 tx->flags |= NETTXF_extra_info;
532 gso->u.gso.size = skb_shinfo(skb)->gso_size;
533 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
534 gso->u.gso.pad = 0;
535 gso->u.gso.features = 0;
537 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
538 gso->flags = 0;
539 extra = gso;
542 np->tx.req_prod_pvt = i + 1;
544 xennet_make_frags(skb, dev, tx);
545 tx->size = skb->len;
547 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
548 if (notify)
549 notify_remote_via_irq(np->netdev->irq);
551 dev->stats.tx_bytes += skb->len;
552 dev->stats.tx_packets++;
554 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
555 xennet_tx_buf_gc(dev);
557 if (!netfront_tx_slot_available(np))
558 netif_stop_queue(dev);
560 spin_unlock_irq(&np->tx_lock);
562 return NETDEV_TX_OK;
564 drop:
565 dev->stats.tx_dropped++;
566 dev_kfree_skb(skb);
567 return NETDEV_TX_OK;
570 static int xennet_close(struct net_device *dev)
572 struct netfront_info *np = netdev_priv(dev);
573 netif_stop_queue(np->netdev);
574 napi_disable(&np->napi);
575 return 0;
578 static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
579 grant_ref_t ref)
581 int new = xennet_rxidx(np->rx.req_prod_pvt);
583 BUG_ON(np->rx_skbs[new]);
584 np->rx_skbs[new] = skb;
585 np->grant_rx_ref[new] = ref;
586 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
587 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
588 np->rx.req_prod_pvt++;
591 static int xennet_get_extras(struct netfront_info *np,
592 struct xen_netif_extra_info *extras,
593 RING_IDX rp)
596 struct xen_netif_extra_info *extra;
597 struct device *dev = &np->netdev->dev;
598 RING_IDX cons = np->rx.rsp_cons;
599 int err = 0;
601 do {
602 struct sk_buff *skb;
603 grant_ref_t ref;
605 if (unlikely(cons + 1 == rp)) {
606 if (net_ratelimit())
607 dev_warn(dev, "Missing extra info\n");
608 err = -EBADR;
609 break;
612 extra = (struct xen_netif_extra_info *)
613 RING_GET_RESPONSE(&np->rx, ++cons);
615 if (unlikely(!extra->type ||
616 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
617 if (net_ratelimit())
618 dev_warn(dev, "Invalid extra type: %d\n",
619 extra->type);
620 err = -EINVAL;
621 } else {
622 memcpy(&extras[extra->type - 1], extra,
623 sizeof(*extra));
626 skb = xennet_get_rx_skb(np, cons);
627 ref = xennet_get_rx_ref(np, cons);
628 xennet_move_rx_slot(np, skb, ref);
629 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
631 np->rx.rsp_cons = cons;
632 return err;
635 static int xennet_get_responses(struct netfront_info *np,
636 struct netfront_rx_info *rinfo, RING_IDX rp,
637 struct sk_buff_head *list)
639 struct xen_netif_rx_response *rx = &rinfo->rx;
640 struct xen_netif_extra_info *extras = rinfo->extras;
641 struct device *dev = &np->netdev->dev;
642 RING_IDX cons = np->rx.rsp_cons;
643 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
644 grant_ref_t ref = xennet_get_rx_ref(np, cons);
645 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
646 int frags = 1;
647 int err = 0;
648 unsigned long ret;
650 if (rx->flags & NETRXF_extra_info) {
651 err = xennet_get_extras(np, extras, rp);
652 cons = np->rx.rsp_cons;
655 for (;;) {
656 if (unlikely(rx->status < 0 ||
657 rx->offset + rx->status > PAGE_SIZE)) {
658 if (net_ratelimit())
659 dev_warn(dev, "rx->offset: %x, size: %u\n",
660 rx->offset, rx->status);
661 xennet_move_rx_slot(np, skb, ref);
662 err = -EINVAL;
663 goto next;
667 * This definitely indicates a bug, either in this driver or in
668 * the backend driver. In future this should flag the bad
669 * situation to the system controller to reboot the backed.
671 if (ref == GRANT_INVALID_REF) {
672 if (net_ratelimit())
673 dev_warn(dev, "Bad rx response id %d.\n",
674 rx->id);
675 err = -EINVAL;
676 goto next;
679 ret = gnttab_end_foreign_access_ref(ref, 0);
680 BUG_ON(!ret);
682 gnttab_release_grant_reference(&np->gref_rx_head, ref);
684 __skb_queue_tail(list, skb);
686 next:
687 if (!(rx->flags & NETRXF_more_data))
688 break;
690 if (cons + frags == rp) {
691 if (net_ratelimit())
692 dev_warn(dev, "Need more frags\n");
693 err = -ENOENT;
694 break;
697 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
698 skb = xennet_get_rx_skb(np, cons + frags);
699 ref = xennet_get_rx_ref(np, cons + frags);
700 frags++;
703 if (unlikely(frags > max)) {
704 if (net_ratelimit())
705 dev_warn(dev, "Too many frags\n");
706 err = -E2BIG;
709 if (unlikely(err))
710 np->rx.rsp_cons = cons + frags;
712 return err;
715 static int xennet_set_skb_gso(struct sk_buff *skb,
716 struct xen_netif_extra_info *gso)
718 if (!gso->u.gso.size) {
719 if (net_ratelimit())
720 printk(KERN_WARNING "GSO size must not be zero.\n");
721 return -EINVAL;
724 /* Currently only TCPv4 S.O. is supported. */
725 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
726 if (net_ratelimit())
727 printk(KERN_WARNING "Bad GSO type %d.\n", gso->u.gso.type);
728 return -EINVAL;
731 skb_shinfo(skb)->gso_size = gso->u.gso.size;
732 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
734 /* Header must be checked, and gso_segs computed. */
735 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
736 skb_shinfo(skb)->gso_segs = 0;
738 return 0;
741 static RING_IDX xennet_fill_frags(struct netfront_info *np,
742 struct sk_buff *skb,
743 struct sk_buff_head *list)
745 struct skb_shared_info *shinfo = skb_shinfo(skb);
746 int nr_frags = shinfo->nr_frags;
747 RING_IDX cons = np->rx.rsp_cons;
748 skb_frag_t *frag = shinfo->frags + nr_frags;
749 struct sk_buff *nskb;
751 while ((nskb = __skb_dequeue(list))) {
752 struct xen_netif_rx_response *rx =
753 RING_GET_RESPONSE(&np->rx, ++cons);
755 frag->page = skb_shinfo(nskb)->frags[0].page;
756 frag->page_offset = rx->offset;
757 frag->size = rx->status;
759 skb->data_len += rx->status;
761 skb_shinfo(nskb)->nr_frags = 0;
762 kfree_skb(nskb);
764 frag++;
765 nr_frags++;
768 shinfo->nr_frags = nr_frags;
769 return cons;
772 static int skb_checksum_setup(struct sk_buff *skb)
774 struct iphdr *iph;
775 unsigned char *th;
776 int err = -EPROTO;
778 if (skb->protocol != htons(ETH_P_IP))
779 goto out;
781 iph = (void *)skb->data;
782 th = skb->data + 4 * iph->ihl;
783 if (th >= skb_tail_pointer(skb))
784 goto out;
786 skb->csum_start = th - skb->head;
787 switch (iph->protocol) {
788 case IPPROTO_TCP:
789 skb->csum_offset = offsetof(struct tcphdr, check);
790 break;
791 case IPPROTO_UDP:
792 skb->csum_offset = offsetof(struct udphdr, check);
793 break;
794 default:
795 if (net_ratelimit())
796 printk(KERN_ERR "Attempting to checksum a non-"
797 "TCP/UDP packet, dropping a protocol"
798 " %d packet", iph->protocol);
799 goto out;
802 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
803 goto out;
805 err = 0;
807 out:
808 return err;
811 static int handle_incoming_queue(struct net_device *dev,
812 struct sk_buff_head *rxq)
814 int packets_dropped = 0;
815 struct sk_buff *skb;
817 while ((skb = __skb_dequeue(rxq)) != NULL) {
818 struct page *page = NETFRONT_SKB_CB(skb)->page;
819 void *vaddr = page_address(page);
820 unsigned offset = NETFRONT_SKB_CB(skb)->offset;
822 memcpy(skb->data, vaddr + offset,
823 skb_headlen(skb));
825 if (page != skb_shinfo(skb)->frags[0].page)
826 __free_page(page);
828 /* Ethernet work: Delayed to here as it peeks the header. */
829 skb->protocol = eth_type_trans(skb, dev);
831 if (skb->ip_summed == CHECKSUM_PARTIAL) {
832 if (skb_checksum_setup(skb)) {
833 kfree_skb(skb);
834 packets_dropped++;
835 dev->stats.rx_errors++;
836 continue;
840 dev->stats.rx_packets++;
841 dev->stats.rx_bytes += skb->len;
843 /* Pass it up. */
844 netif_receive_skb(skb);
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 __napi_complete(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 const struct net_device_ops xennet_netdev_ops = {
1110 .ndo_open = xennet_open,
1111 .ndo_uninit = xennet_uninit,
1112 .ndo_stop = xennet_close,
1113 .ndo_start_xmit = xennet_start_xmit,
1114 .ndo_change_mtu = xennet_change_mtu,
1115 .ndo_set_mac_address = eth_mac_addr,
1116 .ndo_validate_addr = eth_validate_addr,
1119 static struct net_device * __devinit xennet_create_dev(struct xenbus_device *dev)
1121 int i, err;
1122 struct net_device *netdev;
1123 struct netfront_info *np;
1125 netdev = alloc_etherdev(sizeof(struct netfront_info));
1126 if (!netdev) {
1127 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1128 __func__);
1129 return ERR_PTR(-ENOMEM);
1132 np = netdev_priv(netdev);
1133 np->xbdev = dev;
1135 spin_lock_init(&np->tx_lock);
1136 spin_lock_init(&np->rx_lock);
1138 skb_queue_head_init(&np->rx_batch);
1139 np->rx_target = RX_DFL_MIN_TARGET;
1140 np->rx_min_target = RX_DFL_MIN_TARGET;
1141 np->rx_max_target = RX_MAX_TARGET;
1143 init_timer(&np->rx_refill_timer);
1144 np->rx_refill_timer.data = (unsigned long)netdev;
1145 np->rx_refill_timer.function = rx_refill_timeout;
1147 /* Initialise tx_skbs as a free chain containing every entry. */
1148 np->tx_skb_freelist = 0;
1149 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1150 skb_entry_set_link(&np->tx_skbs[i], i+1);
1151 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1154 /* Clear out rx_skbs */
1155 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1156 np->rx_skbs[i] = NULL;
1157 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1160 /* A grant for every tx ring slot */
1161 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1162 &np->gref_tx_head) < 0) {
1163 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1164 err = -ENOMEM;
1165 goto exit;
1167 /* A grant for every rx ring slot */
1168 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1169 &np->gref_rx_head) < 0) {
1170 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1171 err = -ENOMEM;
1172 goto exit_free_tx;
1175 netdev->netdev_ops = &xennet_netdev_ops;
1177 netif_napi_add(netdev, &np->napi, xennet_poll, 64);
1178 netdev->features = NETIF_F_IP_CSUM;
1180 SET_ETHTOOL_OPS(netdev, &xennet_ethtool_ops);
1181 SET_NETDEV_DEV(netdev, &dev->dev);
1183 np->netdev = netdev;
1185 netif_carrier_off(netdev);
1187 return netdev;
1189 exit_free_tx:
1190 gnttab_free_grant_references(np->gref_tx_head);
1191 exit:
1192 free_netdev(netdev);
1193 return ERR_PTR(err);
1197 * Entry point to this code when a new device is created. Allocate the basic
1198 * structures and the ring buffers for communication with the backend, and
1199 * inform the backend of the appropriate details for those.
1201 static int __devinit netfront_probe(struct xenbus_device *dev,
1202 const struct xenbus_device_id *id)
1204 int err;
1205 struct net_device *netdev;
1206 struct netfront_info *info;
1208 netdev = xennet_create_dev(dev);
1209 if (IS_ERR(netdev)) {
1210 err = PTR_ERR(netdev);
1211 xenbus_dev_fatal(dev, err, "creating netdev");
1212 return err;
1215 info = netdev_priv(netdev);
1216 dev_set_drvdata(&dev->dev, info);
1218 err = register_netdev(info->netdev);
1219 if (err) {
1220 printk(KERN_WARNING "%s: register_netdev err=%d\n",
1221 __func__, err);
1222 goto fail;
1225 err = xennet_sysfs_addif(info->netdev);
1226 if (err) {
1227 unregister_netdev(info->netdev);
1228 printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
1229 __func__, err);
1230 goto fail;
1233 return 0;
1235 fail:
1236 free_netdev(netdev);
1237 dev_set_drvdata(&dev->dev, NULL);
1238 return err;
1241 static void xennet_end_access(int ref, void *page)
1243 /* This frees the page as a side-effect */
1244 if (ref != GRANT_INVALID_REF)
1245 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1248 static void xennet_disconnect_backend(struct netfront_info *info)
1250 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1251 spin_lock_bh(&info->rx_lock);
1252 spin_lock_irq(&info->tx_lock);
1253 netif_carrier_off(info->netdev);
1254 spin_unlock_irq(&info->tx_lock);
1255 spin_unlock_bh(&info->rx_lock);
1257 if (info->netdev->irq)
1258 unbind_from_irqhandler(info->netdev->irq, info->netdev);
1259 info->evtchn = info->netdev->irq = 0;
1261 /* End access and free the pages */
1262 xennet_end_access(info->tx_ring_ref, info->tx.sring);
1263 xennet_end_access(info->rx_ring_ref, info->rx.sring);
1265 info->tx_ring_ref = GRANT_INVALID_REF;
1266 info->rx_ring_ref = GRANT_INVALID_REF;
1267 info->tx.sring = NULL;
1268 info->rx.sring = NULL;
1272 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1273 * driver restart. We tear down our netif structure and recreate it, but
1274 * leave the device-layer structures intact so that this is transparent to the
1275 * rest of the kernel.
1277 static int netfront_resume(struct xenbus_device *dev)
1279 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1281 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1283 xennet_disconnect_backend(info);
1284 return 0;
1287 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1289 char *s, *e, *macstr;
1290 int i;
1292 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1293 if (IS_ERR(macstr))
1294 return PTR_ERR(macstr);
1296 for (i = 0; i < ETH_ALEN; i++) {
1297 mac[i] = simple_strtoul(s, &e, 16);
1298 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1299 kfree(macstr);
1300 return -ENOENT;
1302 s = e+1;
1305 kfree(macstr);
1306 return 0;
1309 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1311 struct net_device *dev = dev_id;
1312 struct netfront_info *np = netdev_priv(dev);
1313 unsigned long flags;
1315 spin_lock_irqsave(&np->tx_lock, flags);
1317 if (likely(netif_carrier_ok(dev))) {
1318 xennet_tx_buf_gc(dev);
1319 /* Under tx_lock: protects access to rx shared-ring indexes. */
1320 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
1321 napi_schedule(&np->napi);
1324 spin_unlock_irqrestore(&np->tx_lock, flags);
1326 return IRQ_HANDLED;
1329 static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)
1331 struct xen_netif_tx_sring *txs;
1332 struct xen_netif_rx_sring *rxs;
1333 int err;
1334 struct net_device *netdev = info->netdev;
1336 info->tx_ring_ref = GRANT_INVALID_REF;
1337 info->rx_ring_ref = GRANT_INVALID_REF;
1338 info->rx.sring = NULL;
1339 info->tx.sring = NULL;
1340 netdev->irq = 0;
1342 err = xen_net_read_mac(dev, netdev->dev_addr);
1343 if (err) {
1344 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1345 goto fail;
1348 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1349 if (!txs) {
1350 err = -ENOMEM;
1351 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1352 goto fail;
1354 SHARED_RING_INIT(txs);
1355 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
1357 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
1358 if (err < 0) {
1359 free_page((unsigned long)txs);
1360 goto fail;
1363 info->tx_ring_ref = err;
1364 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1365 if (!rxs) {
1366 err = -ENOMEM;
1367 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1368 goto fail;
1370 SHARED_RING_INIT(rxs);
1371 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
1373 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
1374 if (err < 0) {
1375 free_page((unsigned long)rxs);
1376 goto fail;
1378 info->rx_ring_ref = err;
1380 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1381 if (err)
1382 goto fail;
1384 err = bind_evtchn_to_irqhandler(info->evtchn, xennet_interrupt,
1385 IRQF_SAMPLE_RANDOM, netdev->name,
1386 netdev);
1387 if (err < 0)
1388 goto fail;
1389 netdev->irq = err;
1390 return 0;
1392 fail:
1393 return err;
1396 /* Common code used when first setting up, and when resuming. */
1397 static int talk_to_backend(struct xenbus_device *dev,
1398 struct netfront_info *info)
1400 const char *message;
1401 struct xenbus_transaction xbt;
1402 int err;
1404 /* Create shared ring, alloc event channel. */
1405 err = setup_netfront(dev, info);
1406 if (err)
1407 goto out;
1409 again:
1410 err = xenbus_transaction_start(&xbt);
1411 if (err) {
1412 xenbus_dev_fatal(dev, err, "starting transaction");
1413 goto destroy_ring;
1416 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
1417 info->tx_ring_ref);
1418 if (err) {
1419 message = "writing tx ring-ref";
1420 goto abort_transaction;
1422 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
1423 info->rx_ring_ref);
1424 if (err) {
1425 message = "writing rx ring-ref";
1426 goto abort_transaction;
1428 err = xenbus_printf(xbt, dev->nodename,
1429 "event-channel", "%u", info->evtchn);
1430 if (err) {
1431 message = "writing event-channel";
1432 goto abort_transaction;
1435 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1437 if (err) {
1438 message = "writing request-rx-copy";
1439 goto abort_transaction;
1442 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1443 if (err) {
1444 message = "writing feature-rx-notify";
1445 goto abort_transaction;
1448 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1449 if (err) {
1450 message = "writing feature-sg";
1451 goto abort_transaction;
1454 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1455 if (err) {
1456 message = "writing feature-gso-tcpv4";
1457 goto abort_transaction;
1460 err = xenbus_transaction_end(xbt, 0);
1461 if (err) {
1462 if (err == -EAGAIN)
1463 goto again;
1464 xenbus_dev_fatal(dev, err, "completing transaction");
1465 goto destroy_ring;
1468 return 0;
1470 abort_transaction:
1471 xenbus_transaction_end(xbt, 1);
1472 xenbus_dev_fatal(dev, err, "%s", message);
1473 destroy_ring:
1474 xennet_disconnect_backend(info);
1475 out:
1476 return err;
1479 static int xennet_set_sg(struct net_device *dev, u32 data)
1481 if (data) {
1482 struct netfront_info *np = netdev_priv(dev);
1483 int val;
1485 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1486 "%d", &val) < 0)
1487 val = 0;
1488 if (!val)
1489 return -ENOSYS;
1490 } else if (dev->mtu > ETH_DATA_LEN)
1491 dev->mtu = ETH_DATA_LEN;
1493 return ethtool_op_set_sg(dev, data);
1496 static int xennet_set_tso(struct net_device *dev, u32 data)
1498 if (data) {
1499 struct netfront_info *np = netdev_priv(dev);
1500 int val;
1502 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1503 "feature-gso-tcpv4", "%d", &val) < 0)
1504 val = 0;
1505 if (!val)
1506 return -ENOSYS;
1509 return ethtool_op_set_tso(dev, data);
1512 static void xennet_set_features(struct net_device *dev)
1514 /* Turn off all GSO bits except ROBUST. */
1515 dev->features &= ~NETIF_F_GSO_MASK;
1516 dev->features |= NETIF_F_GSO_ROBUST;
1517 xennet_set_sg(dev, 0);
1519 /* We need checksum offload to enable scatter/gather and TSO. */
1520 if (!(dev->features & NETIF_F_IP_CSUM))
1521 return;
1523 if (!xennet_set_sg(dev, 1))
1524 xennet_set_tso(dev, 1);
1527 static int xennet_connect(struct net_device *dev)
1529 struct netfront_info *np = netdev_priv(dev);
1530 int i, requeue_idx, err;
1531 struct sk_buff *skb;
1532 grant_ref_t ref;
1533 struct xen_netif_rx_request *req;
1534 unsigned int feature_rx_copy;
1536 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1537 "feature-rx-copy", "%u", &feature_rx_copy);
1538 if (err != 1)
1539 feature_rx_copy = 0;
1541 if (!feature_rx_copy) {
1542 dev_info(&dev->dev,
1543 "backend does not support copying receive path\n");
1544 return -ENODEV;
1547 err = talk_to_backend(np->xbdev, np);
1548 if (err)
1549 return err;
1551 xennet_set_features(dev);
1553 spin_lock_bh(&np->rx_lock);
1554 spin_lock_irq(&np->tx_lock);
1556 /* Step 1: Discard all pending TX packet fragments. */
1557 xennet_release_tx_bufs(np);
1559 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1560 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1561 if (!np->rx_skbs[i])
1562 continue;
1564 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1565 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1566 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1568 gnttab_grant_foreign_access_ref(
1569 ref, np->xbdev->otherend_id,
1570 pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
1571 frags->page)),
1573 req->gref = ref;
1574 req->id = requeue_idx;
1576 requeue_idx++;
1579 np->rx.req_prod_pvt = requeue_idx;
1582 * Step 3: All public and private state should now be sane. Get
1583 * ready to start sending and receiving packets and give the driver
1584 * domain a kick because we've probably just requeued some
1585 * packets.
1587 netif_carrier_on(np->netdev);
1588 notify_remote_via_irq(np->netdev->irq);
1589 xennet_tx_buf_gc(dev);
1590 xennet_alloc_rx_buffers(dev);
1592 spin_unlock_irq(&np->tx_lock);
1593 spin_unlock_bh(&np->rx_lock);
1595 return 0;
1599 * Callback received when the backend's state changes.
1601 static void backend_changed(struct xenbus_device *dev,
1602 enum xenbus_state backend_state)
1604 struct netfront_info *np = dev_get_drvdata(&dev->dev);
1605 struct net_device *netdev = np->netdev;
1607 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
1609 switch (backend_state) {
1610 case XenbusStateInitialising:
1611 case XenbusStateInitialised:
1612 case XenbusStateConnected:
1613 case XenbusStateUnknown:
1614 case XenbusStateClosed:
1615 break;
1617 case XenbusStateInitWait:
1618 if (dev->state != XenbusStateInitialising)
1619 break;
1620 if (xennet_connect(netdev) != 0)
1621 break;
1622 xenbus_switch_state(dev, XenbusStateConnected);
1623 break;
1625 case XenbusStateClosing:
1626 xenbus_frontend_closed(dev);
1627 break;
1631 static const struct ethtool_ops xennet_ethtool_ops =
1633 .set_tx_csum = ethtool_op_set_tx_csum,
1634 .set_sg = xennet_set_sg,
1635 .set_tso = xennet_set_tso,
1636 .get_link = ethtool_op_get_link,
1639 #ifdef CONFIG_SYSFS
1640 static ssize_t show_rxbuf_min(struct device *dev,
1641 struct device_attribute *attr, char *buf)
1643 struct net_device *netdev = to_net_dev(dev);
1644 struct netfront_info *info = netdev_priv(netdev);
1646 return sprintf(buf, "%u\n", info->rx_min_target);
1649 static ssize_t store_rxbuf_min(struct device *dev,
1650 struct device_attribute *attr,
1651 const char *buf, size_t len)
1653 struct net_device *netdev = to_net_dev(dev);
1654 struct netfront_info *np = netdev_priv(netdev);
1655 char *endp;
1656 unsigned long target;
1658 if (!capable(CAP_NET_ADMIN))
1659 return -EPERM;
1661 target = simple_strtoul(buf, &endp, 0);
1662 if (endp == buf)
1663 return -EBADMSG;
1665 if (target < RX_MIN_TARGET)
1666 target = RX_MIN_TARGET;
1667 if (target > RX_MAX_TARGET)
1668 target = RX_MAX_TARGET;
1670 spin_lock_bh(&np->rx_lock);
1671 if (target > np->rx_max_target)
1672 np->rx_max_target = target;
1673 np->rx_min_target = target;
1674 if (target > np->rx_target)
1675 np->rx_target = target;
1677 xennet_alloc_rx_buffers(netdev);
1679 spin_unlock_bh(&np->rx_lock);
1680 return len;
1683 static ssize_t show_rxbuf_max(struct device *dev,
1684 struct device_attribute *attr, char *buf)
1686 struct net_device *netdev = to_net_dev(dev);
1687 struct netfront_info *info = netdev_priv(netdev);
1689 return sprintf(buf, "%u\n", info->rx_max_target);
1692 static ssize_t store_rxbuf_max(struct device *dev,
1693 struct device_attribute *attr,
1694 const char *buf, size_t len)
1696 struct net_device *netdev = to_net_dev(dev);
1697 struct netfront_info *np = netdev_priv(netdev);
1698 char *endp;
1699 unsigned long target;
1701 if (!capable(CAP_NET_ADMIN))
1702 return -EPERM;
1704 target = simple_strtoul(buf, &endp, 0);
1705 if (endp == buf)
1706 return -EBADMSG;
1708 if (target < RX_MIN_TARGET)
1709 target = RX_MIN_TARGET;
1710 if (target > RX_MAX_TARGET)
1711 target = RX_MAX_TARGET;
1713 spin_lock_bh(&np->rx_lock);
1714 if (target < np->rx_min_target)
1715 np->rx_min_target = target;
1716 np->rx_max_target = target;
1717 if (target < np->rx_target)
1718 np->rx_target = target;
1720 xennet_alloc_rx_buffers(netdev);
1722 spin_unlock_bh(&np->rx_lock);
1723 return len;
1726 static ssize_t show_rxbuf_cur(struct device *dev,
1727 struct device_attribute *attr, char *buf)
1729 struct net_device *netdev = to_net_dev(dev);
1730 struct netfront_info *info = netdev_priv(netdev);
1732 return sprintf(buf, "%u\n", info->rx_target);
1735 static struct device_attribute xennet_attrs[] = {
1736 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1737 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1738 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1741 static int xennet_sysfs_addif(struct net_device *netdev)
1743 int i;
1744 int err;
1746 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1747 err = device_create_file(&netdev->dev,
1748 &xennet_attrs[i]);
1749 if (err)
1750 goto fail;
1752 return 0;
1754 fail:
1755 while (--i >= 0)
1756 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1757 return err;
1760 static void xennet_sysfs_delif(struct net_device *netdev)
1762 int i;
1764 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
1765 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1768 #endif /* CONFIG_SYSFS */
1770 static struct xenbus_device_id netfront_ids[] = {
1771 { "vif" },
1772 { "" }
1776 static int __devexit xennet_remove(struct xenbus_device *dev)
1778 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1780 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1782 unregister_netdev(info->netdev);
1784 xennet_disconnect_backend(info);
1786 del_timer_sync(&info->rx_refill_timer);
1788 xennet_sysfs_delif(info->netdev);
1790 free_netdev(info->netdev);
1792 return 0;
1795 static struct xenbus_driver netfront_driver = {
1796 .name = "vif",
1797 .owner = THIS_MODULE,
1798 .ids = netfront_ids,
1799 .probe = netfront_probe,
1800 .remove = __devexit_p(xennet_remove),
1801 .resume = netfront_resume,
1802 .otherend_changed = backend_changed,
1805 static int __init netif_init(void)
1807 if (!xen_domain())
1808 return -ENODEV;
1810 if (xen_initial_domain())
1811 return 0;
1813 printk(KERN_INFO "Initialising Xen virtual ethernet driver.\n");
1815 return xenbus_register_frontend(&netfront_driver);
1817 module_init(netif_init);
1820 static void __exit netif_exit(void)
1822 if (xen_initial_domain())
1823 return;
1825 xenbus_unregister_driver(&netfront_driver);
1827 module_exit(netif_exit);
1829 MODULE_DESCRIPTION("Xen virtual network device frontend");
1830 MODULE_LICENSE("GPL");
1831 MODULE_ALIAS("xen:vif");
1832 MODULE_ALIAS("xennet");