ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 10100:aa17b7173325

[NET] front: remove unnecessary include of proc_fs.h.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Thu May 18 21:41:56 2006 +0100 (2006-05-18)
parents cb4d2fc638ec
children b736d3335641
line source
1 /******************************************************************************
2 * Virtual network driver for conversing with remote driver backends.
3 *
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
6 *
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:
12 *
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:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
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.
30 */
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/netdevice.h>
41 #include <linux/inetdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/init.h>
45 #include <linux/bitops.h>
46 #include <linux/ethtool.h>
47 #include <linux/in.h>
48 #include <net/sock.h>
49 #include <net/pkt_sched.h>
50 #include <net/arp.h>
51 #include <net/route.h>
52 #include <asm/io.h>
53 #include <asm/uaccess.h>
54 #include <xen/evtchn.h>
55 #include <xen/xenbus.h>
56 #include <xen/interface/io/netif.h>
57 #include <xen/interface/memory.h>
58 #include <xen/balloon.h>
59 #include <asm/page.h>
60 #include <asm/uaccess.h>
61 #include <xen/interface/grant_table.h>
62 #include <xen/gnttab.h>
63 #include <xen/net_driver_util.h>
65 #define GRANT_INVALID_REF 0
67 #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
68 #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
70 static inline void init_skb_shinfo(struct sk_buff *skb)
71 {
72 atomic_set(&(skb_shinfo(skb)->dataref), 1);
73 skb_shinfo(skb)->nr_frags = 0;
74 skb_shinfo(skb)->frag_list = NULL;
75 }
77 struct netfront_info {
78 struct list_head list;
79 struct net_device *netdev;
81 struct net_device_stats stats;
83 netif_tx_front_ring_t tx;
84 netif_rx_front_ring_t rx;
86 spinlock_t tx_lock;
87 spinlock_t rx_lock;
89 unsigned int handle;
90 unsigned int evtchn, irq;
92 /* What is the status of our connection to the remote backend? */
93 #define BEST_CLOSED 0
94 #define BEST_DISCONNECTED 1
95 #define BEST_CONNECTED 2
96 unsigned int backend_state;
98 /* Receive-ring batched refills. */
99 #define RX_MIN_TARGET 8
100 #define RX_DFL_MIN_TARGET 64
101 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
102 unsigned rx_min_target, rx_max_target, rx_target;
103 struct sk_buff_head rx_batch;
105 struct timer_list rx_refill_timer;
107 /*
108 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
109 * array is an index into a chain of free entries.
110 */
111 struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
112 struct sk_buff *rx_skbs[NET_RX_RING_SIZE+1];
114 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
115 grant_ref_t gref_tx_head;
116 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
117 grant_ref_t gref_rx_head;
118 grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1];
120 struct xenbus_device *xbdev;
121 int tx_ring_ref;
122 int rx_ring_ref;
123 u8 mac[ETH_ALEN];
125 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
126 multicall_entry_t rx_mcl[NET_RX_RING_SIZE+1];
127 mmu_update_t rx_mmu[NET_RX_RING_SIZE];
128 };
130 /*
131 * Access macros for acquiring freeing slots in {tx,rx}_skbs[].
132 */
134 static inline void add_id_to_freelist(struct sk_buff **list, unsigned short id)
135 {
136 list[id] = list[0];
137 list[0] = (void *)(unsigned long)id;
138 }
140 static inline unsigned short get_id_from_freelist(struct sk_buff **list)
141 {
142 unsigned int id = (unsigned int)(unsigned long)list[0];
143 list[0] = list[id];
144 return id;
145 }
147 #ifdef DEBUG
148 static const char *be_state_name[] = {
149 [BEST_CLOSED] = "closed",
150 [BEST_DISCONNECTED] = "disconnected",
151 [BEST_CONNECTED] = "connected",
152 };
153 #endif
155 #define DPRINTK(fmt, args...) pr_debug("netfront (%s:%d) " fmt, \
156 __FUNCTION__, __LINE__, ##args)
157 #define IPRINTK(fmt, args...) \
158 printk(KERN_INFO "netfront: " fmt, ##args)
159 #define WPRINTK(fmt, args...) \
160 printk(KERN_WARNING "netfront: " fmt, ##args)
163 static int talk_to_backend(struct xenbus_device *, struct netfront_info *);
164 static int setup_device(struct xenbus_device *, struct netfront_info *);
165 static struct net_device *create_netdev(int, struct xenbus_device *);
167 static void netfront_closing(struct xenbus_device *);
169 static void end_access(int, void *);
170 static void netif_disconnect_backend(struct netfront_info *);
171 static void close_netdev(struct netfront_info *);
172 static void netif_free(struct netfront_info *);
174 static void show_device(struct netfront_info *);
176 static void network_connect(struct net_device *);
177 static void network_tx_buf_gc(struct net_device *);
178 static void network_alloc_rx_buffers(struct net_device *);
179 static int send_fake_arp(struct net_device *);
181 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
183 #ifdef CONFIG_SYSFS
184 static int xennet_sysfs_addif(struct net_device *netdev);
185 static void xennet_sysfs_delif(struct net_device *netdev);
186 #else /* !CONFIG_SYSFS */
187 #define xennet_sysfs_addif(dev) (0)
188 #define xennet_sysfs_delif(dev) do { } while(0)
189 #endif
191 /**
192 * Entry point to this code when a new device is created. Allocate the basic
193 * structures and the ring buffers for communication with the backend, and
194 * inform the backend of the appropriate details for those. Switch to
195 * Connected state.
196 */
197 static int __devinit netfront_probe(struct xenbus_device *dev,
198 const struct xenbus_device_id *id)
199 {
200 int err;
201 struct net_device *netdev;
202 struct netfront_info *info;
203 unsigned int handle;
205 err = xenbus_scanf(XBT_NULL, dev->nodename, "handle", "%u", &handle);
206 if (err != 1) {
207 xenbus_dev_fatal(dev, err, "reading handle");
208 return err;
209 }
211 netdev = create_netdev(handle, dev);
212 if (IS_ERR(netdev)) {
213 err = PTR_ERR(netdev);
214 xenbus_dev_fatal(dev, err, "creating netdev");
215 return err;
216 }
218 info = netdev_priv(netdev);
219 dev->data = info;
221 err = talk_to_backend(dev, info);
222 if (err) {
223 xennet_sysfs_delif(info->netdev);
224 unregister_netdev(netdev);
225 free_netdev(netdev);
226 dev->data = NULL;
227 return err;
228 }
230 return 0;
231 }
234 /**
235 * We are reconnecting to the backend, due to a suspend/resume, or a backend
236 * driver restart. We tear down our netif structure and recreate it, but
237 * leave the device-layer structures intact so that this is transparent to the
238 * rest of the kernel.
239 */
240 static int netfront_resume(struct xenbus_device *dev)
241 {
242 struct netfront_info *info = dev->data;
244 DPRINTK("%s\n", dev->nodename);
246 netif_disconnect_backend(info);
247 return talk_to_backend(dev, info);
248 }
251 /* Common code used when first setting up, and when resuming. */
252 static int talk_to_backend(struct xenbus_device *dev,
253 struct netfront_info *info)
254 {
255 const char *message;
256 xenbus_transaction_t xbt;
257 int err;
259 err = xen_net_read_mac(dev, info->mac);
260 if (err) {
261 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
262 goto out;
263 }
265 /* Create shared ring, alloc event channel. */
266 err = setup_device(dev, info);
267 if (err)
268 goto out;
270 again:
271 err = xenbus_transaction_start(&xbt);
272 if (err) {
273 xenbus_dev_fatal(dev, err, "starting transaction");
274 goto destroy_ring;
275 }
277 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
278 info->tx_ring_ref);
279 if (err) {
280 message = "writing tx ring-ref";
281 goto abort_transaction;
282 }
283 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
284 info->rx_ring_ref);
285 if (err) {
286 message = "writing rx ring-ref";
287 goto abort_transaction;
288 }
289 err = xenbus_printf(xbt, dev->nodename,
290 "event-channel", "%u", info->evtchn);
291 if (err) {
292 message = "writing event-channel";
293 goto abort_transaction;
294 }
296 err = xenbus_transaction_end(xbt, 0);
297 if (err) {
298 if (err == -EAGAIN)
299 goto again;
300 xenbus_dev_fatal(dev, err, "completing transaction");
301 goto destroy_ring;
302 }
304 xenbus_switch_state(dev, XenbusStateConnected);
306 return 0;
308 abort_transaction:
309 xenbus_transaction_end(xbt, 1);
310 xenbus_dev_fatal(dev, err, "%s", message);
311 destroy_ring:
312 netif_free(info);
313 out:
314 return err;
315 }
318 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
319 {
320 netif_tx_sring_t *txs;
321 netif_rx_sring_t *rxs;
322 int err;
323 struct net_device *netdev = info->netdev;
325 info->tx_ring_ref = GRANT_INVALID_REF;
326 info->rx_ring_ref = GRANT_INVALID_REF;
327 info->rx.sring = NULL;
328 info->tx.sring = NULL;
329 info->irq = 0;
331 txs = (netif_tx_sring_t *)__get_free_page(GFP_KERNEL);
332 if (!txs) {
333 err = -ENOMEM;
334 xenbus_dev_fatal(dev, err, "allocating tx ring page");
335 goto fail;
336 }
337 rxs = (netif_rx_sring_t *)__get_free_page(GFP_KERNEL);
338 if (!rxs) {
339 err = -ENOMEM;
340 xenbus_dev_fatal(dev, err, "allocating rx ring page");
341 goto fail;
342 }
343 memset(txs, 0, PAGE_SIZE);
344 memset(rxs, 0, PAGE_SIZE);
345 info->backend_state = BEST_DISCONNECTED;
347 SHARED_RING_INIT(txs);
348 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
350 SHARED_RING_INIT(rxs);
351 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
353 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
354 if (err < 0)
355 goto fail;
356 info->tx_ring_ref = err;
358 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
359 if (err < 0)
360 goto fail;
361 info->rx_ring_ref = err;
363 err = xenbus_alloc_evtchn(dev, &info->evtchn);
364 if (err)
365 goto fail;
367 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
368 network_connect(netdev);
369 info->irq = bind_evtchn_to_irqhandler(
370 info->evtchn, netif_int, SA_SAMPLE_RANDOM, netdev->name,
371 netdev);
372 (void)send_fake_arp(netdev);
373 show_device(info);
375 return 0;
377 fail:
378 netif_free(info);
379 return err;
380 }
383 /**
384 * Callback received when the backend's state changes.
385 */
386 static void backend_changed(struct xenbus_device *dev,
387 XenbusState backend_state)
388 {
389 DPRINTK("\n");
391 switch (backend_state) {
392 case XenbusStateInitialising:
393 case XenbusStateInitWait:
394 case XenbusStateInitialised:
395 case XenbusStateConnected:
396 case XenbusStateUnknown:
397 case XenbusStateClosed:
398 break;
400 case XenbusStateClosing:
401 netfront_closing(dev);
402 break;
403 }
404 }
407 /** Send a packet on a net device to encourage switches to learn the
408 * MAC. We send a fake ARP request.
409 *
410 * @param dev device
411 * @return 0 on success, error code otherwise
412 */
413 static int send_fake_arp(struct net_device *dev)
414 {
415 struct sk_buff *skb;
416 u32 src_ip, dst_ip;
418 dst_ip = INADDR_BROADCAST;
419 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
421 /* No IP? Then nothing to do. */
422 if (src_ip == 0)
423 return 0;
425 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
426 dst_ip, dev, src_ip,
427 /*dst_hw*/ NULL, /*src_hw*/ NULL,
428 /*target_hw*/ dev->dev_addr);
429 if (skb == NULL)
430 return -ENOMEM;
432 return dev_queue_xmit(skb);
433 }
436 static int network_open(struct net_device *dev)
437 {
438 struct netfront_info *np = netdev_priv(dev);
440 memset(&np->stats, 0, sizeof(np->stats));
442 network_alloc_rx_buffers(dev);
443 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
445 netif_start_queue(dev);
447 return 0;
448 }
450 static inline void network_maybe_wake_tx(struct net_device *dev)
451 {
452 struct netfront_info *np = netdev_priv(dev);
454 if (unlikely(netif_queue_stopped(dev)) &&
455 !RING_FULL(&np->tx) &&
456 !gnttab_empty_grant_references(&np->gref_tx_head) &&
457 likely(netif_running(dev)))
458 netif_wake_queue(dev);
459 }
461 static void network_tx_buf_gc(struct net_device *dev)
462 {
463 RING_IDX cons, prod;
464 unsigned short id;
465 struct netfront_info *np = netdev_priv(dev);
466 struct sk_buff *skb;
468 if (np->backend_state != BEST_CONNECTED)
469 return;
471 do {
472 prod = np->tx.sring->rsp_prod;
473 rmb(); /* Ensure we see responses up to 'rp'. */
475 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
476 id = RING_GET_RESPONSE(&np->tx, cons)->id;
477 skb = np->tx_skbs[id];
478 if (unlikely(gnttab_query_foreign_access(
479 np->grant_tx_ref[id]) != 0)) {
480 printk(KERN_ALERT "network_tx_buf_gc: warning "
481 "-- grant still in use by backend "
482 "domain.\n");
483 break; /* bail immediately */
484 }
485 gnttab_end_foreign_access_ref(
486 np->grant_tx_ref[id], GNTMAP_readonly);
487 gnttab_release_grant_reference(
488 &np->gref_tx_head, np->grant_tx_ref[id]);
489 np->grant_tx_ref[id] = GRANT_INVALID_REF;
490 add_id_to_freelist(np->tx_skbs, id);
491 dev_kfree_skb_irq(skb);
492 }
494 np->tx.rsp_cons = prod;
496 /*
497 * Set a new event, then check for race with update of tx_cons.
498 * Note that it is essential to schedule a callback, no matter
499 * how few buffers are pending. Even if there is space in the
500 * transmit ring, higher layers may be blocked because too much
501 * data is outstanding: in such cases notification from Xen is
502 * likely to be the only kick that we'll get.
503 */
504 np->tx.sring->rsp_event =
505 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
506 mb();
507 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
509 network_maybe_wake_tx(dev);
510 }
513 static void rx_refill_timeout(unsigned long data)
514 {
515 struct net_device *dev = (struct net_device *)data;
516 netif_rx_schedule(dev);
517 }
520 static void network_alloc_rx_buffers(struct net_device *dev)
521 {
522 unsigned short id;
523 struct netfront_info *np = netdev_priv(dev);
524 struct sk_buff *skb;
525 int i, batch_target;
526 RING_IDX req_prod = np->rx.req_prod_pvt;
527 struct xen_memory_reservation reservation;
528 grant_ref_t ref;
530 if (unlikely(np->backend_state != BEST_CONNECTED))
531 return;
533 /*
534 * Allocate skbuffs greedily, even though we batch updates to the
535 * receive ring. This creates a less bursty demand on the memory
536 * allocator, so should reduce the chance of failed allocation requests
537 * both for ourself and for other kernel subsystems.
538 */
539 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
540 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
541 /*
542 * Subtract dev_alloc_skb headroom (16 bytes) and shared info
543 * tailroom then round down to SKB_DATA_ALIGN boundary.
544 */
545 skb = __dev_alloc_skb(
546 ((PAGE_SIZE - sizeof(struct skb_shared_info)) &
547 (-SKB_DATA_ALIGN(1))) - 16,
548 GFP_ATOMIC|__GFP_NOWARN);
549 if (skb == NULL) {
550 /* Any skbuffs queued for refill? Force them out. */
551 if (i != 0)
552 goto refill;
553 /* Could not allocate any skbuffs. Try again later. */
554 mod_timer(&np->rx_refill_timer,
555 jiffies + (HZ/10));
556 return;
557 }
558 __skb_queue_tail(&np->rx_batch, skb);
559 }
561 /* Is the batch large enough to be worthwhile? */
562 if (i < (np->rx_target/2))
563 return;
565 /* Adjust our fill target if we risked running out of buffers. */
566 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
567 ((np->rx_target *= 2) > np->rx_max_target))
568 np->rx_target = np->rx_max_target;
570 refill:
571 for (i = 0; ; i++) {
572 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
573 break;
575 skb->dev = dev;
577 id = get_id_from_freelist(np->rx_skbs);
579 np->rx_skbs[id] = skb;
581 RING_GET_REQUEST(&np->rx, req_prod + i)->id = id;
582 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
583 BUG_ON((signed short)ref < 0);
584 np->grant_rx_ref[id] = ref;
585 gnttab_grant_foreign_transfer_ref(ref,
586 np->xbdev->otherend_id,
587 __pa(skb->head) >> PAGE_SHIFT);
588 RING_GET_REQUEST(&np->rx, req_prod + i)->gref = ref;
589 np->rx_pfn_array[i] = virt_to_mfn(skb->head);
591 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
592 /* Remove this page before passing back to Xen. */
593 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
594 INVALID_P2M_ENTRY);
595 MULTI_update_va_mapping(np->rx_mcl+i,
596 (unsigned long)skb->head,
597 __pte(0), 0);
598 }
599 }
601 /* Tell the ballon driver what is going on. */
602 balloon_update_driver_allowance(i);
604 set_xen_guest_handle(reservation.extent_start, np->rx_pfn_array);
605 reservation.nr_extents = i;
606 reservation.extent_order = 0;
607 reservation.address_bits = 0;
608 reservation.domid = DOMID_SELF;
610 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
611 /* After all PTEs have been zapped, flush the TLB. */
612 np->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
613 UVMF_TLB_FLUSH|UVMF_ALL;
615 /* Give away a batch of pages. */
616 np->rx_mcl[i].op = __HYPERVISOR_memory_op;
617 np->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
618 np->rx_mcl[i].args[1] = (unsigned long)&reservation;
620 /* Zap PTEs and give away pages in one big multicall. */
621 (void)HYPERVISOR_multicall(np->rx_mcl, i+1);
623 /* Check return status of HYPERVISOR_memory_op(). */
624 if (unlikely(np->rx_mcl[i].result != i))
625 panic("Unable to reduce memory reservation\n");
626 } else
627 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
628 &reservation) != i)
629 panic("Unable to reduce memory reservation\n");
631 /* Above is a suitable barrier to ensure backend will see requests. */
632 np->rx.req_prod_pvt = req_prod + i;
633 RING_PUSH_REQUESTS(&np->rx);
634 }
637 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
638 {
639 unsigned short id;
640 struct netfront_info *np = netdev_priv(dev);
641 netif_tx_request_t *tx;
642 RING_IDX i;
643 grant_ref_t ref;
644 unsigned long mfn;
645 int notify;
647 if (unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
648 PAGE_SIZE)) {
649 struct sk_buff *nskb;
650 nskb = __dev_alloc_skb(skb->len, GFP_ATOMIC|__GFP_NOWARN);
651 if (unlikely(nskb == NULL))
652 goto drop;
653 skb_put(nskb, skb->len);
654 memcpy(nskb->data, skb->data, skb->len);
655 /* Copy only the header fields we use in this driver. */
656 nskb->dev = skb->dev;
657 nskb->ip_summed = skb->ip_summed;
658 nskb->proto_data_valid = skb->proto_data_valid;
659 dev_kfree_skb(skb);
660 skb = nskb;
661 }
663 spin_lock_irq(&np->tx_lock);
665 if (np->backend_state != BEST_CONNECTED) {
666 spin_unlock_irq(&np->tx_lock);
667 goto drop;
668 }
670 i = np->tx.req_prod_pvt;
672 id = get_id_from_freelist(np->tx_skbs);
673 np->tx_skbs[id] = skb;
675 tx = RING_GET_REQUEST(&np->tx, i);
677 tx->id = id;
678 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
679 BUG_ON((signed short)ref < 0);
680 mfn = virt_to_mfn(skb->data);
681 gnttab_grant_foreign_access_ref(
682 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
683 tx->gref = np->grant_tx_ref[id] = ref;
684 tx->offset = (unsigned long)skb->data & ~PAGE_MASK;
685 tx->size = skb->len;
687 tx->flags = 0;
688 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
689 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
690 if (skb->proto_data_valid) /* remote but checksummed? */
691 tx->flags |= NETTXF_data_validated;
693 np->tx.req_prod_pvt = i + 1;
694 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
695 if (notify)
696 notify_remote_via_irq(np->irq);
698 network_tx_buf_gc(dev);
700 if (RING_FULL(&np->tx) ||
701 gnttab_empty_grant_references(&np->gref_tx_head))
702 netif_stop_queue(dev);
704 spin_unlock_irq(&np->tx_lock);
706 np->stats.tx_bytes += skb->len;
707 np->stats.tx_packets++;
709 return 0;
711 drop:
712 np->stats.tx_dropped++;
713 dev_kfree_skb(skb);
714 return 0;
715 }
717 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
718 {
719 struct net_device *dev = dev_id;
720 struct netfront_info *np = netdev_priv(dev);
721 unsigned long flags;
723 spin_lock_irqsave(&np->tx_lock, flags);
724 network_tx_buf_gc(dev);
725 spin_unlock_irqrestore(&np->tx_lock, flags);
727 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx) &&
728 likely(netif_running(dev)))
729 netif_rx_schedule(dev);
731 return IRQ_HANDLED;
732 }
735 static int netif_poll(struct net_device *dev, int *pbudget)
736 {
737 struct netfront_info *np = netdev_priv(dev);
738 struct sk_buff *skb, *nskb;
739 netif_rx_response_t *rx;
740 RING_IDX i, rp;
741 mmu_update_t *mmu = np->rx_mmu;
742 multicall_entry_t *mcl = np->rx_mcl;
743 int work_done, budget, more_to_do = 1;
744 struct sk_buff_head rxq;
745 unsigned long flags;
746 unsigned long mfn;
747 grant_ref_t ref;
749 spin_lock(&np->rx_lock);
751 if (np->backend_state != BEST_CONNECTED) {
752 spin_unlock(&np->rx_lock);
753 return 0;
754 }
756 skb_queue_head_init(&rxq);
758 if ((budget = *pbudget) > dev->quota)
759 budget = dev->quota;
760 rp = np->rx.sring->rsp_prod;
761 rmb(); /* Ensure we see queued responses up to 'rp'. */
763 for (i = np->rx.rsp_cons, work_done = 0;
764 (i != rp) && (work_done < budget);
765 i++, work_done++) {
766 rx = RING_GET_RESPONSE(&np->rx, i);
768 /*
769 * This definitely indicates a bug, either in this driver or
770 * in the backend driver. In future this should flag the bad
771 * situation to the system controller to reboot the backed.
772 */
773 if ((ref = np->grant_rx_ref[rx->id]) == GRANT_INVALID_REF) {
774 WPRINTK("Bad rx response id %d.\n", rx->id);
775 work_done--;
776 continue;
777 }
779 /* Memory pressure, insufficient buffer headroom, ... */
780 if ((mfn = gnttab_end_foreign_transfer_ref(ref)) == 0) {
781 if (net_ratelimit())
782 WPRINTK("Unfulfilled rx req (id=%d, st=%d).\n",
783 rx->id, rx->status);
784 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id =
785 rx->id;
786 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref =
787 ref;
788 np->rx.req_prod_pvt++;
789 RING_PUSH_REQUESTS(&np->rx);
790 work_done--;
791 continue;
792 }
794 gnttab_release_grant_reference(&np->gref_rx_head, ref);
795 np->grant_rx_ref[rx->id] = GRANT_INVALID_REF;
797 skb = np->rx_skbs[rx->id];
798 add_id_to_freelist(np->rx_skbs, rx->id);
800 /* NB. We handle skb overflow later. */
801 skb->data = skb->head + rx->offset;
802 skb->len = rx->status;
803 skb->tail = skb->data + skb->len;
805 /*
806 * Old backends do not assert data_validated but we
807 * can infer it from csum_blank so test both flags.
808 */
809 if (rx->flags & (NETRXF_data_validated|NETRXF_csum_blank)) {
810 skb->ip_summed = CHECKSUM_UNNECESSARY;
811 skb->proto_data_valid = 1;
812 } else {
813 skb->ip_summed = CHECKSUM_NONE;
814 skb->proto_data_valid = 0;
815 }
816 skb->proto_csum_blank = !!(rx->flags & NETRXF_csum_blank);
818 np->stats.rx_packets++;
819 np->stats.rx_bytes += rx->status;
821 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
822 /* Remap the page. */
823 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
824 pfn_pte_ma(mfn, PAGE_KERNEL),
825 0);
826 mcl++;
827 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
828 | MMU_MACHPHYS_UPDATE;
829 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
830 mmu++;
832 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
833 mfn);
834 }
836 __skb_queue_tail(&rxq, skb);
837 }
839 /* Some pages are no longer absent... */
840 balloon_update_driver_allowance(-work_done);
842 /* Do all the remapping work, and M2P updates, in one big hypercall. */
843 if (likely((mcl - np->rx_mcl) != 0)) {
844 mcl->op = __HYPERVISOR_mmu_update;
845 mcl->args[0] = (unsigned long)np->rx_mmu;
846 mcl->args[1] = mmu - np->rx_mmu;
847 mcl->args[2] = 0;
848 mcl->args[3] = DOMID_SELF;
849 mcl++;
850 (void)HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
851 }
853 while ((skb = __skb_dequeue(&rxq)) != NULL) {
854 if (skb->len > (dev->mtu + ETH_HLEN + 4)) {
855 if (net_ratelimit())
856 printk(KERN_INFO "Received packet too big for "
857 "MTU (%d > %d)\n",
858 skb->len - ETH_HLEN - 4, dev->mtu);
859 skb->len = 0;
860 skb->tail = skb->data;
861 init_skb_shinfo(skb);
862 dev_kfree_skb(skb);
863 continue;
864 }
866 /*
867 * Enough room in skbuff for the data we were passed? Also,
868 * Linux expects at least 16 bytes headroom in each rx buffer.
869 */
870 if (unlikely(skb->tail > skb->end) ||
871 unlikely((skb->data - skb->head) < 16)) {
872 if (net_ratelimit()) {
873 if (skb->tail > skb->end)
874 printk(KERN_INFO "Received packet "
875 "is %zd bytes beyond tail.\n",
876 skb->tail - skb->end);
877 else
878 printk(KERN_INFO "Received packet "
879 "is %zd bytes before head.\n",
880 16 - (skb->data - skb->head));
881 }
883 nskb = __dev_alloc_skb(skb->len + 2,
884 GFP_ATOMIC|__GFP_NOWARN);
885 if (nskb != NULL) {
886 skb_reserve(nskb, 2);
887 skb_put(nskb, skb->len);
888 memcpy(nskb->data, skb->data, skb->len);
889 /* Copy any other fields we already set up. */
890 nskb->dev = skb->dev;
891 nskb->ip_summed = skb->ip_summed;
892 nskb->proto_data_valid = skb->proto_data_valid;
893 nskb->proto_csum_blank = skb->proto_csum_blank;
894 }
896 /* Reinitialise and then destroy the old skbuff. */
897 skb->len = 0;
898 skb->tail = skb->data;
899 init_skb_shinfo(skb);
900 dev_kfree_skb(skb);
902 /* Switch old for new, if we copied the buffer. */
903 if ((skb = nskb) == NULL)
904 continue;
905 }
907 /* Set the shinfo area, which is hidden behind the data. */
908 init_skb_shinfo(skb);
909 /* Ethernet work: Delayed to here as it peeks the header. */
910 skb->protocol = eth_type_trans(skb, dev);
912 /* Pass it up. */
913 netif_receive_skb(skb);
914 dev->last_rx = jiffies;
915 }
917 np->rx.rsp_cons = i;
919 /* If we get a callback with very few responses, reduce fill target. */
920 /* NB. Note exponential increase, linear decrease. */
921 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
922 ((3*np->rx_target) / 4)) &&
923 (--np->rx_target < np->rx_min_target))
924 np->rx_target = np->rx_min_target;
926 network_alloc_rx_buffers(dev);
928 *pbudget -= work_done;
929 dev->quota -= work_done;
931 if (work_done < budget) {
932 local_irq_save(flags);
934 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
935 if (!more_to_do)
936 __netif_rx_complete(dev);
938 local_irq_restore(flags);
939 }
941 spin_unlock(&np->rx_lock);
943 return more_to_do;
944 }
947 static int network_close(struct net_device *dev)
948 {
949 struct netfront_info *np = netdev_priv(dev);
950 netif_stop_queue(np->netdev);
951 return 0;
952 }
955 static struct net_device_stats *network_get_stats(struct net_device *dev)
956 {
957 struct netfront_info *np = netdev_priv(dev);
958 return &np->stats;
959 }
961 static void network_connect(struct net_device *dev)
962 {
963 struct netfront_info *np;
964 int i, requeue_idx;
965 netif_tx_request_t *tx;
966 struct sk_buff *skb;
968 np = netdev_priv(dev);
969 spin_lock_irq(&np->tx_lock);
970 spin_lock(&np->rx_lock);
972 /* Recovery procedure: */
974 /*
975 * Step 1: Rebuild the RX and TX ring contents.
976 * NB. We could just free the queued TX packets now but we hope
977 * that sending them out might do some good. We have to rebuild
978 * the RX ring because some of our pages are currently flipped out
979 * so we can't just free the RX skbs.
980 * NB2. Freelist index entries are always going to be less than
981 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
982 * greater than PAGE_OFFSET: we use this property to distinguish
983 * them.
984 */
986 /*
987 * Rebuild the TX buffer freelist and the TX ring itself.
988 * NB. This reorders packets. We could keep more private state
989 * to avoid this but maybe it doesn't matter so much given the
990 * interface has been down.
991 */
992 for (requeue_idx = 0, i = 1; i <= NET_TX_RING_SIZE; i++) {
993 if ((unsigned long)np->tx_skbs[i] < PAGE_OFFSET)
994 continue;
996 skb = np->tx_skbs[i];
998 tx = RING_GET_REQUEST(&np->tx, requeue_idx);
999 requeue_idx++;
1001 tx->id = i;
1002 gnttab_grant_foreign_access_ref(
1003 np->grant_tx_ref[i], np->xbdev->otherend_id,
1004 virt_to_mfn(np->tx_skbs[i]->data),
1005 GNTMAP_readonly);
1006 tx->gref = np->grant_tx_ref[i];
1007 tx->offset = (unsigned long)skb->data & ~PAGE_MASK;
1008 tx->size = skb->len;
1009 tx->flags = 0;
1010 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
1011 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
1012 if (skb->proto_data_valid) /* remote but checksummed? */
1013 tx->flags |= NETTXF_data_validated;
1015 np->stats.tx_bytes += skb->len;
1016 np->stats.tx_packets++;
1019 np->tx.req_prod_pvt = requeue_idx;
1020 RING_PUSH_REQUESTS(&np->tx);
1022 /* Rebuild the RX buffer freelist and the RX ring itself. */
1023 for (requeue_idx = 0, i = 1; i <= NET_RX_RING_SIZE; i++) {
1024 if ((unsigned long)np->rx_skbs[i] < PAGE_OFFSET)
1025 continue;
1026 gnttab_grant_foreign_transfer_ref(
1027 np->grant_rx_ref[i], np->xbdev->otherend_id,
1028 __pa(np->rx_skbs[i]->data) >> PAGE_SHIFT);
1029 RING_GET_REQUEST(&np->rx, requeue_idx)->gref =
1030 np->grant_rx_ref[i];
1031 RING_GET_REQUEST(&np->rx, requeue_idx)->id = i;
1032 requeue_idx++;
1035 np->rx.req_prod_pvt = requeue_idx;
1036 RING_PUSH_REQUESTS(&np->rx);
1038 /*
1039 * Step 2: All public and private state should now be sane. Get
1040 * ready to start sending and receiving packets and give the driver
1041 * domain a kick because we've probably just requeued some
1042 * packets.
1043 */
1044 np->backend_state = BEST_CONNECTED;
1045 notify_remote_via_irq(np->irq);
1046 network_tx_buf_gc(dev);
1048 network_maybe_wake_tx(dev);
1050 spin_unlock(&np->rx_lock);
1051 spin_unlock_irq(&np->tx_lock);
1054 static void show_device(struct netfront_info *np)
1056 #ifdef DEBUG
1057 if (np) {
1058 IPRINTK("<vif handle=%u %s(%s) evtchn=%u tx=%p rx=%p>\n",
1059 np->handle,
1060 be_state_name[np->backend_state],
1061 netif_running(np->netdev) ? "open" : "closed",
1062 np->evtchn,
1063 np->tx,
1064 np->rx);
1065 } else
1066 IPRINTK("<vif NULL>\n");
1067 #endif
1070 static void netif_uninit(struct net_device *dev)
1072 struct netfront_info *np = netdev_priv(dev);
1073 gnttab_free_grant_references(np->gref_tx_head);
1074 gnttab_free_grant_references(np->gref_rx_head);
1077 static struct ethtool_ops network_ethtool_ops =
1079 .get_tx_csum = ethtool_op_get_tx_csum,
1080 .set_tx_csum = ethtool_op_set_tx_csum,
1081 };
1083 #ifdef CONFIG_SYSFS
1084 static ssize_t show_rxbuf_min(struct class_device *cd, char *buf)
1086 struct net_device *netdev = container_of(cd, struct net_device,
1087 class_dev);
1088 struct netfront_info *info = netdev_priv(netdev);
1090 return sprintf(buf, "%u\n", info->rx_min_target);
1093 static ssize_t store_rxbuf_min(struct class_device *cd,
1094 const char *buf, size_t len)
1096 struct net_device *netdev = container_of(cd, struct net_device,
1097 class_dev);
1098 struct netfront_info *np = netdev_priv(netdev);
1099 char *endp;
1100 unsigned long target;
1102 if (!capable(CAP_NET_ADMIN))
1103 return -EPERM;
1105 target = simple_strtoul(buf, &endp, 0);
1106 if (endp == buf)
1107 return -EBADMSG;
1109 if (target < RX_MIN_TARGET)
1110 target = RX_MIN_TARGET;
1111 if (target > RX_MAX_TARGET)
1112 target = RX_MAX_TARGET;
1114 spin_lock(&np->rx_lock);
1115 if (target > np->rx_max_target)
1116 np->rx_max_target = target;
1117 np->rx_min_target = target;
1118 if (target > np->rx_target)
1119 np->rx_target = target;
1121 network_alloc_rx_buffers(netdev);
1123 spin_unlock(&np->rx_lock);
1124 return len;
1127 static ssize_t show_rxbuf_max(struct class_device *cd, char *buf)
1129 struct net_device *netdev = container_of(cd, struct net_device,
1130 class_dev);
1131 struct netfront_info *info = netdev_priv(netdev);
1133 return sprintf(buf, "%u\n", info->rx_max_target);
1136 static ssize_t store_rxbuf_max(struct class_device *cd,
1137 const char *buf, size_t len)
1139 struct net_device *netdev = container_of(cd, struct net_device,
1140 class_dev);
1141 struct netfront_info *np = netdev_priv(netdev);
1142 char *endp;
1143 unsigned long target;
1145 if (!capable(CAP_NET_ADMIN))
1146 return -EPERM;
1148 target = simple_strtoul(buf, &endp, 0);
1149 if (endp == buf)
1150 return -EBADMSG;
1152 if (target < RX_MIN_TARGET)
1153 target = RX_MIN_TARGET;
1154 if (target > RX_MAX_TARGET)
1155 target = RX_MAX_TARGET;
1157 spin_lock(&np->rx_lock);
1158 if (target < np->rx_min_target)
1159 np->rx_min_target = target;
1160 np->rx_max_target = target;
1161 if (target < np->rx_target)
1162 np->rx_target = target;
1164 network_alloc_rx_buffers(netdev);
1166 spin_unlock(&np->rx_lock);
1167 return len;
1170 static ssize_t show_rxbuf_cur(struct class_device *cd, char *buf)
1172 struct net_device *netdev = container_of(cd, struct net_device,
1173 class_dev);
1174 struct netfront_info *info = netdev_priv(netdev);
1176 return sprintf(buf, "%u\n", info->rx_target);
1179 static const struct class_device_attribute xennet_attrs[] = {
1180 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1181 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1182 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1183 };
1185 static int xennet_sysfs_addif(struct net_device *netdev)
1187 int i;
1188 int error = 0;
1190 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1191 error = class_device_create_file(&netdev->class_dev,
1192 &xennet_attrs[i]);
1193 if (error)
1194 goto fail;
1196 return 0;
1198 fail:
1199 while (--i >= 0)
1200 class_device_remove_file(&netdev->class_dev,
1201 &xennet_attrs[i]);
1202 return error;
1205 static void xennet_sysfs_delif(struct net_device *netdev)
1207 int i;
1209 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1210 class_device_remove_file(&netdev->class_dev,
1211 &xennet_attrs[i]);
1215 #endif /* CONFIG_SYSFS */
1218 /*
1219 * Nothing to do here. Virtual interface is point-to-point and the
1220 * physical interface is probably promiscuous anyway.
1221 */
1222 static void network_set_multicast_list(struct net_device *dev)
1226 /** Create a network device.
1227 * @param handle device handle
1228 * @param val return parameter for created device
1229 * @return 0 on success, error code otherwise
1230 */
1231 static struct net_device * __devinit create_netdev(int handle,
1232 struct xenbus_device *dev)
1234 int i, err = 0;
1235 struct net_device *netdev = NULL;
1236 struct netfront_info *np = NULL;
1238 netdev = alloc_etherdev(sizeof(struct netfront_info));
1239 if (!netdev) {
1240 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1241 __FUNCTION__);
1242 return ERR_PTR(-ENOMEM);
1245 np = netdev_priv(netdev);
1246 np->backend_state = BEST_CLOSED;
1247 np->handle = handle;
1248 np->xbdev = dev;
1250 spin_lock_init(&np->tx_lock);
1251 spin_lock_init(&np->rx_lock);
1253 skb_queue_head_init(&np->rx_batch);
1254 np->rx_target = RX_DFL_MIN_TARGET;
1255 np->rx_min_target = RX_DFL_MIN_TARGET;
1256 np->rx_max_target = RX_MAX_TARGET;
1258 init_timer(&np->rx_refill_timer);
1259 np->rx_refill_timer.data = (unsigned long)netdev;
1260 np->rx_refill_timer.function = rx_refill_timeout;
1262 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
1263 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1264 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1265 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1268 for (i = 0; i <= NET_RX_RING_SIZE; i++) {
1269 np->rx_skbs[i] = (void *)((unsigned long) i+1);
1270 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1273 /* A grant for every tx ring slot */
1274 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1275 &np->gref_tx_head) < 0) {
1276 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1277 err = -ENOMEM;
1278 goto exit;
1280 /* A grant for every rx ring slot */
1281 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1282 &np->gref_rx_head) < 0) {
1283 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1284 gnttab_free_grant_references(np->gref_tx_head);
1285 err = -ENOMEM;
1286 goto exit_free_tx;
1289 netdev->open = network_open;
1290 netdev->hard_start_xmit = network_start_xmit;
1291 netdev->stop = network_close;
1292 netdev->get_stats = network_get_stats;
1293 netdev->poll = netif_poll;
1294 netdev->set_multicast_list = network_set_multicast_list;
1295 netdev->uninit = netif_uninit;
1296 netdev->weight = 64;
1297 netdev->features = NETIF_F_IP_CSUM;
1299 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
1300 SET_MODULE_OWNER(netdev);
1301 SET_NETDEV_DEV(netdev, &dev->dev);
1303 err = register_netdev(netdev);
1304 if (err) {
1305 printk(KERN_WARNING "%s> register_netdev err=%d\n",
1306 __FUNCTION__, err);
1307 goto exit_free_rx;
1310 err = xennet_sysfs_addif(netdev);
1311 if (err) {
1312 /* This can be non-fatal: it only means no tuning parameters */
1313 printk(KERN_WARNING "%s> add sysfs failed err=%d\n",
1314 __FUNCTION__, err);
1317 np->netdev = netdev;
1319 return netdev;
1322 exit_free_rx:
1323 gnttab_free_grant_references(np->gref_rx_head);
1324 exit_free_tx:
1325 gnttab_free_grant_references(np->gref_tx_head);
1326 exit:
1327 free_netdev(netdev);
1328 return ERR_PTR(err);
1331 /*
1332 * We use this notifier to send out a fake ARP reply to reset switches and
1333 * router ARP caches when an IP interface is brought up on a VIF.
1334 */
1335 static int
1336 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1338 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1339 struct net_device *dev = ifa->ifa_dev->dev;
1341 /* UP event and is it one of our devices? */
1342 if (event == NETDEV_UP && dev->open == network_open)
1343 (void)send_fake_arp(dev);
1345 return NOTIFY_DONE;
1349 /* ** Close down ** */
1352 /**
1353 * Handle the change of state of the backend to Closing. We must delete our
1354 * device-layer structures now, to ensure that writes are flushed through to
1355 * the backend. Once is this done, we can switch to Closed in
1356 * acknowledgement.
1357 */
1358 static void netfront_closing(struct xenbus_device *dev)
1360 struct netfront_info *info = dev->data;
1362 DPRINTK("netfront_closing: %s removed\n", dev->nodename);
1364 close_netdev(info);
1366 xenbus_switch_state(dev, XenbusStateClosed);
1370 static int __devexit netfront_remove(struct xenbus_device *dev)
1372 struct netfront_info *info = dev->data;
1374 DPRINTK("%s\n", dev->nodename);
1376 netif_disconnect_backend(info);
1377 free_netdev(info->netdev);
1379 return 0;
1383 static void close_netdev(struct netfront_info *info)
1385 del_timer_sync(&info->rx_refill_timer);
1387 xennet_sysfs_delif(info->netdev);
1388 unregister_netdev(info->netdev);
1392 static void netif_disconnect_backend(struct netfront_info *info)
1394 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1395 spin_lock_irq(&info->tx_lock);
1396 spin_lock(&info->rx_lock);
1397 info->backend_state = BEST_DISCONNECTED;
1398 spin_unlock(&info->rx_lock);
1399 spin_unlock_irq(&info->tx_lock);
1401 if (info->irq)
1402 unbind_from_irqhandler(info->irq, info->netdev);
1403 info->evtchn = info->irq = 0;
1405 end_access(info->tx_ring_ref, info->tx.sring);
1406 end_access(info->rx_ring_ref, info->rx.sring);
1407 info->tx_ring_ref = GRANT_INVALID_REF;
1408 info->rx_ring_ref = GRANT_INVALID_REF;
1409 info->tx.sring = NULL;
1410 info->rx.sring = NULL;
1414 static void netif_free(struct netfront_info *info)
1416 close_netdev(info);
1417 netif_disconnect_backend(info);
1418 free_netdev(info->netdev);
1422 static void end_access(int ref, void *page)
1424 if (ref != GRANT_INVALID_REF)
1425 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1429 /* ** Driver registration ** */
1432 static struct xenbus_device_id netfront_ids[] = {
1433 { "vif" },
1434 { "" }
1435 };
1438 static struct xenbus_driver netfront = {
1439 .name = "vif",
1440 .owner = THIS_MODULE,
1441 .ids = netfront_ids,
1442 .probe = netfront_probe,
1443 .remove = __devexit_p(netfront_remove),
1444 .resume = netfront_resume,
1445 .otherend_changed = backend_changed,
1446 };
1449 static struct notifier_block notifier_inetdev = {
1450 .notifier_call = inetdev_notify,
1451 .next = NULL,
1452 .priority = 0
1453 };
1455 static int __init netif_init(void)
1457 if (xen_start_info->flags & SIF_INITDOMAIN)
1458 return 0;
1460 IPRINTK("Initialising virtual ethernet driver.\n");
1462 (void)register_inetaddr_notifier(&notifier_inetdev);
1464 return xenbus_register_frontend(&netfront);
1466 module_init(netif_init);
1469 static void __exit netif_exit(void)
1471 unregister_inetaddr_notifier(&notifier_inetdev);
1473 return xenbus_unregister_driver(&netfront);
1475 module_exit(netif_exit);
1477 MODULE_LICENSE("Dual BSD/GPL");