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