ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 10313:3913a119477d

[NET] front: Turn grant-ref error into a fatal bug. That's the best we
can do until we have a backend driver recovery method.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@dhcp93.uk.xensource.com
date Tue Jun 06 08:21:31 2006 +0100 (2006-06-06)
parents aecdb4c52fa7
children 2dea01abfbfe
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 <linux/if_ether.h>
49 #include <net/sock.h>
50 #include <net/pkt_sched.h>
51 #include <net/arp.h>
52 #include <net/route.h>
53 #include <asm/io.h>
54 #include <asm/uaccess.h>
55 #include <xen/evtchn.h>
56 #include <xen/xenbus.h>
57 #include <xen/interface/io/netif.h>
58 #include <xen/interface/memory.h>
59 #include <xen/balloon.h>
60 #include <asm/page.h>
61 #include <asm/uaccess.h>
62 #include <xen/interface/grant_table.h>
63 #include <xen/gnttab.h>
65 #define GRANT_INVALID_REF 0
67 #define NET_TX_RING_SIZE __RING_SIZE((struct netif_tx_sring *)0, PAGE_SIZE)
68 #define NET_RX_RING_SIZE __RING_SIZE((struct netif_rx_sring *)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 struct netif_tx_front_ring tx;
84 struct netif_rx_front_ring rx;
86 spinlock_t tx_lock;
87 spinlock_t rx_lock;
89 unsigned int handle;
90 unsigned int evtchn, irq;
92 /* Receive-ring batched refills. */
93 #define RX_MIN_TARGET 8
94 #define RX_DFL_MIN_TARGET 64
95 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
96 unsigned rx_min_target, rx_max_target, rx_target;
97 struct sk_buff_head rx_batch;
99 struct timer_list rx_refill_timer;
101 /*
102 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
103 * array is an index into a chain of free entries.
104 */
105 struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
106 struct sk_buff *rx_skbs[NET_RX_RING_SIZE+1];
108 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
109 grant_ref_t gref_tx_head;
110 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
111 grant_ref_t gref_rx_head;
112 grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1];
114 struct xenbus_device *xbdev;
115 int tx_ring_ref;
116 int rx_ring_ref;
117 u8 mac[ETH_ALEN];
119 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
120 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
121 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
122 };
124 /*
125 * Access macros for acquiring freeing slots in {tx,rx}_skbs[].
126 */
128 static inline void add_id_to_freelist(struct sk_buff **list, unsigned short id)
129 {
130 list[id] = list[0];
131 list[0] = (void *)(unsigned long)id;
132 }
134 static inline unsigned short get_id_from_freelist(struct sk_buff **list)
135 {
136 unsigned int id = (unsigned int)(unsigned long)list[0];
137 list[0] = list[id];
138 return id;
139 }
141 #define DPRINTK(fmt, args...) pr_debug("netfront (%s:%d) " fmt, \
142 __FUNCTION__, __LINE__, ##args)
143 #define IPRINTK(fmt, args...) \
144 printk(KERN_INFO "netfront: " fmt, ##args)
145 #define WPRINTK(fmt, args...) \
146 printk(KERN_WARNING "netfront: " fmt, ##args)
149 static int talk_to_backend(struct xenbus_device *, struct netfront_info *);
150 static int setup_device(struct xenbus_device *, struct netfront_info *);
151 static struct net_device *create_netdev(int, struct xenbus_device *);
153 static void netfront_closing(struct xenbus_device *);
155 static void end_access(int, void *);
156 static void netif_disconnect_backend(struct netfront_info *);
157 static void close_netdev(struct netfront_info *);
158 static void netif_free(struct netfront_info *);
160 static void show_device(struct netfront_info *);
162 static void network_connect(struct net_device *);
163 static void network_tx_buf_gc(struct net_device *);
164 static void network_alloc_rx_buffers(struct net_device *);
165 static int send_fake_arp(struct net_device *);
167 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
169 #ifdef CONFIG_SYSFS
170 static int xennet_sysfs_addif(struct net_device *netdev);
171 static void xennet_sysfs_delif(struct net_device *netdev);
172 #else /* !CONFIG_SYSFS */
173 #define xennet_sysfs_addif(dev) (0)
174 #define xennet_sysfs_delif(dev) do { } while(0)
175 #endif
177 static inline int xennet_can_sg(struct net_device *dev)
178 {
179 return dev->features & NETIF_F_SG;
180 }
182 /**
183 * Entry point to this code when a new device is created. Allocate the basic
184 * structures and the ring buffers for communication with the backend, and
185 * inform the backend of the appropriate details for those. Switch to
186 * Connected state.
187 */
188 static int __devinit netfront_probe(struct xenbus_device *dev,
189 const struct xenbus_device_id *id)
190 {
191 int err;
192 struct net_device *netdev;
193 struct netfront_info *info;
194 unsigned int handle;
196 err = xenbus_scanf(XBT_NULL, dev->nodename, "handle", "%u", &handle);
197 if (err != 1) {
198 xenbus_dev_fatal(dev, err, "reading handle");
199 return err;
200 }
202 netdev = create_netdev(handle, dev);
203 if (IS_ERR(netdev)) {
204 err = PTR_ERR(netdev);
205 xenbus_dev_fatal(dev, err, "creating netdev");
206 return err;
207 }
209 info = netdev_priv(netdev);
210 dev->data = info;
212 err = talk_to_backend(dev, info);
213 if (err) {
214 xennet_sysfs_delif(info->netdev);
215 unregister_netdev(netdev);
216 free_netdev(netdev);
217 dev->data = NULL;
218 return err;
219 }
221 return 0;
222 }
225 /**
226 * We are reconnecting to the backend, due to a suspend/resume, or a backend
227 * driver restart. We tear down our netif structure and recreate it, but
228 * leave the device-layer structures intact so that this is transparent to the
229 * rest of the kernel.
230 */
231 static int netfront_resume(struct xenbus_device *dev)
232 {
233 struct netfront_info *info = dev->data;
235 DPRINTK("%s\n", dev->nodename);
237 netif_disconnect_backend(info);
238 return talk_to_backend(dev, info);
239 }
241 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
242 {
243 char *s, *e, *macstr;
244 int i;
246 macstr = s = xenbus_read(XBT_NULL, dev->nodename, "mac", NULL);
247 if (IS_ERR(macstr))
248 return PTR_ERR(macstr);
250 for (i = 0; i < ETH_ALEN; i++) {
251 mac[i] = simple_strtoul(s, &e, 16);
252 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
253 kfree(macstr);
254 return -ENOENT;
255 }
256 s = e+1;
257 }
259 kfree(macstr);
260 return 0;
261 }
263 /* Common code used when first setting up, and when resuming. */
264 static int talk_to_backend(struct xenbus_device *dev,
265 struct netfront_info *info)
266 {
267 const char *message;
268 xenbus_transaction_t xbt;
269 int err;
271 err = xen_net_read_mac(dev, info->mac);
272 if (err) {
273 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
274 goto out;
275 }
277 /* Create shared ring, alloc event channel. */
278 err = setup_device(dev, info);
279 if (err)
280 goto out;
282 again:
283 err = xenbus_transaction_start(&xbt);
284 if (err) {
285 xenbus_dev_fatal(dev, err, "starting transaction");
286 goto destroy_ring;
287 }
289 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
290 info->tx_ring_ref);
291 if (err) {
292 message = "writing tx ring-ref";
293 goto abort_transaction;
294 }
295 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
296 info->rx_ring_ref);
297 if (err) {
298 message = "writing rx ring-ref";
299 goto abort_transaction;
300 }
301 err = xenbus_printf(xbt, dev->nodename,
302 "event-channel", "%u", info->evtchn);
303 if (err) {
304 message = "writing event-channel";
305 goto abort_transaction;
306 }
308 err = xenbus_transaction_end(xbt, 0);
309 if (err) {
310 if (err == -EAGAIN)
311 goto again;
312 xenbus_dev_fatal(dev, err, "completing transaction");
313 goto destroy_ring;
314 }
316 return 0;
318 abort_transaction:
319 xenbus_transaction_end(xbt, 1);
320 xenbus_dev_fatal(dev, err, "%s", message);
321 destroy_ring:
322 netif_free(info);
323 out:
324 return err;
325 }
328 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
329 {
330 struct netif_tx_sring *txs;
331 struct netif_rx_sring *rxs;
332 int err;
333 struct net_device *netdev = info->netdev;
335 info->tx_ring_ref = GRANT_INVALID_REF;
336 info->rx_ring_ref = GRANT_INVALID_REF;
337 info->rx.sring = NULL;
338 info->tx.sring = NULL;
339 info->irq = 0;
341 txs = (struct netif_tx_sring *)__get_free_page(GFP_KERNEL);
342 if (!txs) {
343 err = -ENOMEM;
344 xenbus_dev_fatal(dev, err, "allocating tx ring page");
345 goto fail;
346 }
347 rxs = (struct netif_rx_sring *)__get_free_page(GFP_KERNEL);
348 if (!rxs) {
349 err = -ENOMEM;
350 xenbus_dev_fatal(dev, err, "allocating rx ring page");
351 goto fail;
352 }
353 memset(txs, 0, PAGE_SIZE);
354 memset(rxs, 0, PAGE_SIZE);
356 SHARED_RING_INIT(txs);
357 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
359 SHARED_RING_INIT(rxs);
360 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
362 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
363 if (err < 0)
364 goto fail;
365 info->tx_ring_ref = err;
367 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
368 if (err < 0)
369 goto fail;
370 info->rx_ring_ref = err;
372 err = xenbus_alloc_evtchn(dev, &info->evtchn);
373 if (err)
374 goto fail;
376 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
377 info->irq = bind_evtchn_to_irqhandler(
378 info->evtchn, netif_int, SA_SAMPLE_RANDOM, netdev->name,
379 netdev);
381 return 0;
383 fail:
384 netif_free(info);
385 return err;
386 }
389 /**
390 * Callback received when the backend's state changes.
391 */
392 static void backend_changed(struct xenbus_device *dev,
393 enum xenbus_state backend_state)
394 {
395 struct netfront_info *np = dev->data;
396 struct net_device *netdev = np->netdev;
398 DPRINTK("\n");
400 switch (backend_state) {
401 case XenbusStateInitialising:
402 case XenbusStateInitialised:
403 case XenbusStateConnected:
404 case XenbusStateUnknown:
405 case XenbusStateClosed:
406 break;
408 case XenbusStateInitWait:
409 network_connect(netdev);
410 xenbus_switch_state(dev, XenbusStateConnected);
411 (void)send_fake_arp(netdev);
412 show_device(np);
413 break;
415 case XenbusStateClosing:
416 netfront_closing(dev);
417 break;
418 }
419 }
422 /** Send a packet on a net device to encourage switches to learn the
423 * MAC. We send a fake ARP request.
424 *
425 * @param dev device
426 * @return 0 on success, error code otherwise
427 */
428 static int send_fake_arp(struct net_device *dev)
429 {
430 struct sk_buff *skb;
431 u32 src_ip, dst_ip;
433 dst_ip = INADDR_BROADCAST;
434 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
436 /* No IP? Then nothing to do. */
437 if (src_ip == 0)
438 return 0;
440 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
441 dst_ip, dev, src_ip,
442 /*dst_hw*/ NULL, /*src_hw*/ NULL,
443 /*target_hw*/ dev->dev_addr);
444 if (skb == NULL)
445 return -ENOMEM;
447 return dev_queue_xmit(skb);
448 }
451 static int network_open(struct net_device *dev)
452 {
453 struct netfront_info *np = netdev_priv(dev);
455 memset(&np->stats, 0, sizeof(np->stats));
457 network_alloc_rx_buffers(dev);
458 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
460 netif_start_queue(dev);
462 return 0;
463 }
465 static inline int netfront_tx_slot_available(struct netfront_info *np)
466 {
467 return RING_FREE_REQUESTS(&np->tx) >= MAX_SKB_FRAGS + 1;
468 }
470 static inline void network_maybe_wake_tx(struct net_device *dev)
471 {
472 struct netfront_info *np = netdev_priv(dev);
474 if (unlikely(netif_queue_stopped(dev)) &&
475 netfront_tx_slot_available(np) &&
476 likely(netif_running(dev)))
477 netif_wake_queue(dev);
478 }
480 static void network_tx_buf_gc(struct net_device *dev)
481 {
482 RING_IDX cons, prod;
483 unsigned short id;
484 struct netfront_info *np = netdev_priv(dev);
485 struct sk_buff *skb;
487 if (unlikely(!netif_carrier_ok(dev)))
488 return;
490 do {
491 prod = np->tx.sring->rsp_prod;
492 rmb(); /* Ensure we see responses up to 'rp'. */
494 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
495 id = RING_GET_RESPONSE(&np->tx, cons)->id;
496 skb = np->tx_skbs[id];
497 if (unlikely(gnttab_query_foreign_access(
498 np->grant_tx_ref[id]) != 0)) {
499 printk(KERN_ALERT "network_tx_buf_gc: warning "
500 "-- grant still in use by backend "
501 "domain.\n");
502 BUG();
503 }
504 gnttab_end_foreign_access_ref(
505 np->grant_tx_ref[id], GNTMAP_readonly);
506 gnttab_release_grant_reference(
507 &np->gref_tx_head, np->grant_tx_ref[id]);
508 np->grant_tx_ref[id] = GRANT_INVALID_REF;
509 add_id_to_freelist(np->tx_skbs, id);
510 dev_kfree_skb_irq(skb);
511 }
513 np->tx.rsp_cons = prod;
515 /*
516 * Set a new event, then check for race with update of tx_cons.
517 * Note that it is essential to schedule a callback, no matter
518 * how few buffers are pending. Even if there is space in the
519 * transmit ring, higher layers may be blocked because too much
520 * data is outstanding: in such cases notification from Xen is
521 * likely to be the only kick that we'll get.
522 */
523 np->tx.sring->rsp_event =
524 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
525 mb();
526 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
528 network_maybe_wake_tx(dev);
529 }
532 static void rx_refill_timeout(unsigned long data)
533 {
534 struct net_device *dev = (struct net_device *)data;
535 netif_rx_schedule(dev);
536 }
539 static void network_alloc_rx_buffers(struct net_device *dev)
540 {
541 unsigned short id;
542 struct netfront_info *np = netdev_priv(dev);
543 struct sk_buff *skb;
544 int i, batch_target;
545 RING_IDX req_prod = np->rx.req_prod_pvt;
546 struct xen_memory_reservation reservation;
547 grant_ref_t ref;
549 if (unlikely(!netif_carrier_ok(dev)))
550 return;
552 /*
553 * Allocate skbuffs greedily, even though we batch updates to the
554 * receive ring. This creates a less bursty demand on the memory
555 * allocator, so should reduce the chance of failed allocation requests
556 * both for ourself and for other kernel subsystems.
557 */
558 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
559 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
560 /*
561 * Subtract dev_alloc_skb headroom (16 bytes) and shared info
562 * tailroom then round down to SKB_DATA_ALIGN boundary.
563 */
564 skb = __dev_alloc_skb(
565 ((PAGE_SIZE - sizeof(struct skb_shared_info)) &
566 (-SKB_DATA_ALIGN(1))) - 16,
567 GFP_ATOMIC|__GFP_NOWARN);
568 if (skb == NULL) {
569 /* Any skbuffs queued for refill? Force them out. */
570 if (i != 0)
571 goto refill;
572 /* Could not allocate any skbuffs. Try again later. */
573 mod_timer(&np->rx_refill_timer,
574 jiffies + (HZ/10));
575 return;
576 }
577 __skb_queue_tail(&np->rx_batch, skb);
578 }
580 /* Is the batch large enough to be worthwhile? */
581 if (i < (np->rx_target/2))
582 return;
584 /* Adjust our fill target if we risked running out of buffers. */
585 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
586 ((np->rx_target *= 2) > np->rx_max_target))
587 np->rx_target = np->rx_max_target;
589 refill:
590 for (i = 0; ; i++) {
591 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
592 break;
594 skb->dev = dev;
596 id = get_id_from_freelist(np->rx_skbs);
598 np->rx_skbs[id] = skb;
600 RING_GET_REQUEST(&np->rx, req_prod + i)->id = id;
601 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
602 BUG_ON((signed short)ref < 0);
603 np->grant_rx_ref[id] = ref;
604 gnttab_grant_foreign_transfer_ref(ref,
605 np->xbdev->otherend_id,
606 __pa(skb->head) >> PAGE_SHIFT);
607 RING_GET_REQUEST(&np->rx, req_prod + i)->gref = ref;
608 np->rx_pfn_array[i] = virt_to_mfn(skb->head);
610 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
611 /* Remove this page before passing back to Xen. */
612 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
613 INVALID_P2M_ENTRY);
614 MULTI_update_va_mapping(np->rx_mcl+i,
615 (unsigned long)skb->head,
616 __pte(0), 0);
617 }
618 }
620 /* Tell the ballon driver what is going on. */
621 balloon_update_driver_allowance(i);
623 set_xen_guest_handle(reservation.extent_start, np->rx_pfn_array);
624 reservation.nr_extents = i;
625 reservation.extent_order = 0;
626 reservation.address_bits = 0;
627 reservation.domid = DOMID_SELF;
629 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
630 /* After all PTEs have been zapped, flush the TLB. */
631 np->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
632 UVMF_TLB_FLUSH|UVMF_ALL;
634 /* Give away a batch of pages. */
635 np->rx_mcl[i].op = __HYPERVISOR_memory_op;
636 np->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
637 np->rx_mcl[i].args[1] = (unsigned long)&reservation;
639 /* Zap PTEs and give away pages in one big multicall. */
640 (void)HYPERVISOR_multicall(np->rx_mcl, i+1);
642 /* Check return status of HYPERVISOR_memory_op(). */
643 if (unlikely(np->rx_mcl[i].result != i))
644 panic("Unable to reduce memory reservation\n");
645 } else
646 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
647 &reservation) != i)
648 panic("Unable to reduce memory reservation\n");
650 /* Above is a suitable barrier to ensure backend will see requests. */
651 np->rx.req_prod_pvt = req_prod + i;
652 RING_PUSH_REQUESTS(&np->rx);
653 }
655 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
656 struct netif_tx_request *tx)
657 {
658 struct netfront_info *np = netdev_priv(dev);
659 char *data = skb->data;
660 unsigned long mfn;
661 RING_IDX prod = np->tx.req_prod_pvt;
662 int frags = skb_shinfo(skb)->nr_frags;
663 unsigned int offset = offset_in_page(data);
664 unsigned int len = skb_headlen(skb);
665 unsigned int id;
666 grant_ref_t ref;
667 int i;
669 while (len > PAGE_SIZE - offset) {
670 tx->size = PAGE_SIZE - offset;
671 tx->flags |= NETTXF_more_data;
672 len -= tx->size;
673 data += tx->size;
674 offset = 0;
676 id = get_id_from_freelist(np->tx_skbs);
677 np->tx_skbs[id] = skb_get(skb);
678 tx = RING_GET_REQUEST(&np->tx, prod++);
679 tx->id = id;
680 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
681 BUG_ON((signed short)ref < 0);
683 mfn = virt_to_mfn(data);
684 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
685 mfn, GNTMAP_readonly);
687 tx->gref = np->grant_tx_ref[id] = ref;
688 tx->offset = offset;
689 tx->size = len;
690 tx->flags = 0;
691 }
693 for (i = 0; i < frags; i++) {
694 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
696 tx->flags |= NETTXF_more_data;
698 id = get_id_from_freelist(np->tx_skbs);
699 np->tx_skbs[id] = skb_get(skb);
700 tx = RING_GET_REQUEST(&np->tx, prod++);
701 tx->id = id;
702 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
703 BUG_ON((signed short)ref < 0);
705 mfn = pfn_to_mfn(page_to_pfn(frag->page));
706 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
707 mfn, GNTMAP_readonly);
709 tx->gref = np->grant_tx_ref[id] = ref;
710 tx->offset = frag->page_offset;
711 tx->size = frag->size;
712 tx->flags = 0;
713 }
715 np->tx.req_prod_pvt = prod;
716 }
718 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
719 {
720 unsigned short id;
721 struct netfront_info *np = netdev_priv(dev);
722 struct netif_tx_request *tx;
723 char *data = skb->data;
724 RING_IDX i;
725 grant_ref_t ref;
726 unsigned long mfn;
727 int notify;
728 int frags = skb_shinfo(skb)->nr_frags;
729 unsigned int offset = offset_in_page(data);
730 unsigned int len = skb_headlen(skb);
732 frags += (offset + len + PAGE_SIZE - 1) / PAGE_SIZE;
733 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
734 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
735 frags);
736 dump_stack();
737 goto drop;
738 }
740 spin_lock_irq(&np->tx_lock);
742 if (unlikely(!netif_carrier_ok(dev) ||
743 (frags > 1 && !xennet_can_sg(dev)))) {
744 spin_unlock_irq(&np->tx_lock);
745 goto drop;
746 }
748 i = np->tx.req_prod_pvt;
750 id = get_id_from_freelist(np->tx_skbs);
751 np->tx_skbs[id] = skb;
753 tx = RING_GET_REQUEST(&np->tx, i);
755 tx->id = id;
756 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
757 BUG_ON((signed short)ref < 0);
758 mfn = virt_to_mfn(data);
759 gnttab_grant_foreign_access_ref(
760 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
761 tx->gref = np->grant_tx_ref[id] = ref;
762 tx->offset = offset;
763 tx->size = len;
765 tx->flags = 0;
766 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
767 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
768 if (skb->proto_data_valid) /* remote but checksummed? */
769 tx->flags |= NETTXF_data_validated;
771 np->tx.req_prod_pvt = i + 1;
773 xennet_make_frags(skb, dev, tx);
774 tx->size = skb->len;
776 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
777 if (notify)
778 notify_remote_via_irq(np->irq);
780 network_tx_buf_gc(dev);
782 if (!netfront_tx_slot_available(np))
783 netif_stop_queue(dev);
785 spin_unlock_irq(&np->tx_lock);
787 np->stats.tx_bytes += skb->len;
788 np->stats.tx_packets++;
790 return 0;
792 drop:
793 np->stats.tx_dropped++;
794 dev_kfree_skb(skb);
795 return 0;
796 }
798 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
799 {
800 struct net_device *dev = dev_id;
801 struct netfront_info *np = netdev_priv(dev);
802 unsigned long flags;
804 spin_lock_irqsave(&np->tx_lock, flags);
805 network_tx_buf_gc(dev);
806 spin_unlock_irqrestore(&np->tx_lock, flags);
808 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx) &&
809 likely(netif_running(dev)))
810 netif_rx_schedule(dev);
812 return IRQ_HANDLED;
813 }
816 static int netif_poll(struct net_device *dev, int *pbudget)
817 {
818 struct netfront_info *np = netdev_priv(dev);
819 struct sk_buff *skb, *nskb;
820 struct netif_rx_response *rx;
821 RING_IDX i, rp;
822 struct mmu_update *mmu = np->rx_mmu;
823 struct multicall_entry *mcl = np->rx_mcl;
824 int work_done, budget, more_to_do = 1;
825 struct sk_buff_head rxq;
826 unsigned long flags;
827 unsigned long mfn;
828 grant_ref_t ref;
830 spin_lock(&np->rx_lock);
832 if (unlikely(!netif_carrier_ok(dev))) {
833 spin_unlock(&np->rx_lock);
834 return 0;
835 }
837 skb_queue_head_init(&rxq);
839 if ((budget = *pbudget) > dev->quota)
840 budget = dev->quota;
841 rp = np->rx.sring->rsp_prod;
842 rmb(); /* Ensure we see queued responses up to 'rp'. */
844 for (i = np->rx.rsp_cons, work_done = 0;
845 (i != rp) && (work_done < budget);
846 i++, work_done++) {
847 rx = RING_GET_RESPONSE(&np->rx, i);
849 /*
850 * This definitely indicates a bug, either in this driver or
851 * in the backend driver. In future this should flag the bad
852 * situation to the system controller to reboot the backed.
853 */
854 if ((ref = np->grant_rx_ref[rx->id]) == GRANT_INVALID_REF) {
855 WPRINTK("Bad rx response id %d.\n", rx->id);
856 work_done--;
857 continue;
858 }
860 /* Memory pressure, insufficient buffer headroom, ... */
861 if ((mfn = gnttab_end_foreign_transfer_ref(ref)) == 0) {
862 if (net_ratelimit())
863 WPRINTK("Unfulfilled rx req (id=%d, st=%d).\n",
864 rx->id, rx->status);
865 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id =
866 rx->id;
867 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref =
868 ref;
869 np->rx.req_prod_pvt++;
870 RING_PUSH_REQUESTS(&np->rx);
871 work_done--;
872 continue;
873 }
875 gnttab_release_grant_reference(&np->gref_rx_head, ref);
876 np->grant_rx_ref[rx->id] = GRANT_INVALID_REF;
878 skb = np->rx_skbs[rx->id];
879 add_id_to_freelist(np->rx_skbs, rx->id);
881 /* NB. We handle skb overflow later. */
882 skb->data = skb->head + rx->offset;
883 skb->len = rx->status;
884 skb->tail = skb->data + skb->len;
886 /*
887 * Old backends do not assert data_validated but we
888 * can infer it from csum_blank so test both flags.
889 */
890 if (rx->flags & (NETRXF_data_validated|NETRXF_csum_blank)) {
891 skb->ip_summed = CHECKSUM_UNNECESSARY;
892 skb->proto_data_valid = 1;
893 } else {
894 skb->ip_summed = CHECKSUM_NONE;
895 skb->proto_data_valid = 0;
896 }
897 skb->proto_csum_blank = !!(rx->flags & NETRXF_csum_blank);
899 np->stats.rx_packets++;
900 np->stats.rx_bytes += rx->status;
902 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
903 /* Remap the page. */
904 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
905 pfn_pte_ma(mfn, PAGE_KERNEL),
906 0);
907 mcl++;
908 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
909 | MMU_MACHPHYS_UPDATE;
910 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
911 mmu++;
913 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
914 mfn);
915 }
917 __skb_queue_tail(&rxq, skb);
918 }
920 /* Some pages are no longer absent... */
921 balloon_update_driver_allowance(-work_done);
923 /* Do all the remapping work, and M2P updates, in one big hypercall. */
924 if (likely((mcl - np->rx_mcl) != 0)) {
925 mcl->op = __HYPERVISOR_mmu_update;
926 mcl->args[0] = (unsigned long)np->rx_mmu;
927 mcl->args[1] = mmu - np->rx_mmu;
928 mcl->args[2] = 0;
929 mcl->args[3] = DOMID_SELF;
930 mcl++;
931 (void)HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
932 }
934 while ((skb = __skb_dequeue(&rxq)) != NULL) {
935 if (skb->len > (dev->mtu + ETH_HLEN + 4)) {
936 if (net_ratelimit())
937 printk(KERN_INFO "Received packet too big for "
938 "MTU (%d > %d)\n",
939 skb->len - ETH_HLEN - 4, dev->mtu);
940 skb->len = 0;
941 skb->tail = skb->data;
942 init_skb_shinfo(skb);
943 dev_kfree_skb(skb);
944 continue;
945 }
947 /*
948 * Enough room in skbuff for the data we were passed? Also,
949 * Linux expects at least 16 bytes headroom in each rx buffer.
950 */
951 if (unlikely(skb->tail > skb->end) ||
952 unlikely((skb->data - skb->head) < 16)) {
953 if (net_ratelimit()) {
954 if (skb->tail > skb->end)
955 printk(KERN_INFO "Received packet "
956 "is %zd bytes beyond tail.\n",
957 skb->tail - skb->end);
958 else
959 printk(KERN_INFO "Received packet "
960 "is %zd bytes before head.\n",
961 16 - (skb->data - skb->head));
962 }
964 nskb = __dev_alloc_skb(skb->len + 2,
965 GFP_ATOMIC|__GFP_NOWARN);
966 if (nskb != NULL) {
967 skb_reserve(nskb, 2);
968 skb_put(nskb, skb->len);
969 memcpy(nskb->data, skb->data, skb->len);
970 /* Copy any other fields we already set up. */
971 nskb->dev = skb->dev;
972 nskb->ip_summed = skb->ip_summed;
973 nskb->proto_data_valid = skb->proto_data_valid;
974 nskb->proto_csum_blank = skb->proto_csum_blank;
975 }
977 /* Reinitialise and then destroy the old skbuff. */
978 skb->len = 0;
979 skb->tail = skb->data;
980 init_skb_shinfo(skb);
981 dev_kfree_skb(skb);
983 /* Switch old for new, if we copied the buffer. */
984 if ((skb = nskb) == NULL)
985 continue;
986 }
988 /* Set the shinfo area, which is hidden behind the data. */
989 init_skb_shinfo(skb);
990 /* Ethernet work: Delayed to here as it peeks the header. */
991 skb->protocol = eth_type_trans(skb, dev);
993 /* Pass it up. */
994 netif_receive_skb(skb);
995 dev->last_rx = jiffies;
996 }
998 np->rx.rsp_cons = i;
1000 /* If we get a callback with very few responses, reduce fill target. */
1001 /* NB. Note exponential increase, linear decrease. */
1002 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
1003 ((3*np->rx_target) / 4)) &&
1004 (--np->rx_target < np->rx_min_target))
1005 np->rx_target = np->rx_min_target;
1007 network_alloc_rx_buffers(dev);
1009 *pbudget -= work_done;
1010 dev->quota -= work_done;
1012 if (work_done < budget) {
1013 local_irq_save(flags);
1015 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
1016 if (!more_to_do)
1017 __netif_rx_complete(dev);
1019 local_irq_restore(flags);
1022 spin_unlock(&np->rx_lock);
1024 return more_to_do;
1028 static int network_close(struct net_device *dev)
1030 struct netfront_info *np = netdev_priv(dev);
1031 netif_stop_queue(np->netdev);
1032 return 0;
1036 static struct net_device_stats *network_get_stats(struct net_device *dev)
1038 struct netfront_info *np = netdev_priv(dev);
1039 return &np->stats;
1042 static int xennet_change_mtu(struct net_device *dev, int mtu)
1044 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
1046 if (mtu > max)
1047 return -EINVAL;
1048 dev->mtu = mtu;
1049 return 0;
1052 static int xennet_set_sg(struct net_device *dev, u32 data)
1054 if (data) {
1055 struct netfront_info *np = netdev_priv(dev);
1056 int val;
1058 if (xenbus_scanf(XBT_NULL, np->xbdev->otherend, "feature-sg",
1059 "%d", &val) < 0)
1060 val = 0;
1061 if (!val)
1062 return -ENOSYS;
1063 } else if (dev->mtu > ETH_DATA_LEN)
1064 dev->mtu = ETH_DATA_LEN;
1066 return ethtool_op_set_sg(dev, data);
1069 static void xennet_set_features(struct net_device *dev)
1071 xennet_set_sg(dev, 1);
1074 static void network_connect(struct net_device *dev)
1076 struct netfront_info *np;
1077 int i, requeue_idx;
1078 struct netif_tx_request *tx;
1079 struct sk_buff *skb;
1081 xennet_set_features(dev);
1083 np = netdev_priv(dev);
1084 spin_lock_irq(&np->tx_lock);
1085 spin_lock(&np->rx_lock);
1087 /* Recovery procedure: */
1089 /*
1090 * Step 1: Rebuild the RX and TX ring contents.
1091 * NB. We could just free the queued TX packets now but we hope
1092 * that sending them out might do some good. We have to rebuild
1093 * the RX ring because some of our pages are currently flipped out
1094 * so we can't just free the RX skbs.
1095 * NB2. Freelist index entries are always going to be less than
1096 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
1097 * greater than PAGE_OFFSET: we use this property to distinguish
1098 * them.
1099 */
1101 /*
1102 * Rebuild the TX buffer freelist and the TX ring itself.
1103 * NB. This reorders packets. We could keep more private state
1104 * to avoid this but maybe it doesn't matter so much given the
1105 * interface has been down.
1106 */
1107 for (requeue_idx = 0, i = 1; i <= NET_TX_RING_SIZE; i++) {
1108 if ((unsigned long)np->tx_skbs[i] < PAGE_OFFSET)
1109 continue;
1111 skb = np->tx_skbs[i];
1113 tx = RING_GET_REQUEST(&np->tx, requeue_idx);
1114 requeue_idx++;
1116 tx->id = i;
1117 gnttab_grant_foreign_access_ref(
1118 np->grant_tx_ref[i], np->xbdev->otherend_id,
1119 virt_to_mfn(np->tx_skbs[i]->data),
1120 GNTMAP_readonly);
1121 tx->gref = np->grant_tx_ref[i];
1122 tx->offset = (unsigned long)skb->data & ~PAGE_MASK;
1123 tx->size = skb->len;
1124 tx->flags = 0;
1125 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
1126 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
1127 if (skb->proto_data_valid) /* remote but checksummed? */
1128 tx->flags |= NETTXF_data_validated;
1130 np->stats.tx_bytes += skb->len;
1131 np->stats.tx_packets++;
1134 np->tx.req_prod_pvt = requeue_idx;
1135 RING_PUSH_REQUESTS(&np->tx);
1137 /* Rebuild the RX buffer freelist and the RX ring itself. */
1138 for (requeue_idx = 0, i = 1; i <= NET_RX_RING_SIZE; i++) {
1139 if ((unsigned long)np->rx_skbs[i] < PAGE_OFFSET)
1140 continue;
1141 gnttab_grant_foreign_transfer_ref(
1142 np->grant_rx_ref[i], np->xbdev->otherend_id,
1143 __pa(np->rx_skbs[i]->data) >> PAGE_SHIFT);
1144 RING_GET_REQUEST(&np->rx, requeue_idx)->gref =
1145 np->grant_rx_ref[i];
1146 RING_GET_REQUEST(&np->rx, requeue_idx)->id = i;
1147 requeue_idx++;
1150 np->rx.req_prod_pvt = requeue_idx;
1151 RING_PUSH_REQUESTS(&np->rx);
1153 /*
1154 * Step 2: All public and private state should now be sane. Get
1155 * ready to start sending and receiving packets and give the driver
1156 * domain a kick because we've probably just requeued some
1157 * packets.
1158 */
1159 netif_carrier_on(dev);
1160 notify_remote_via_irq(np->irq);
1161 network_tx_buf_gc(dev);
1163 spin_unlock(&np->rx_lock);
1164 spin_unlock_irq(&np->tx_lock);
1167 static void show_device(struct netfront_info *np)
1169 #ifdef DEBUG
1170 if (np) {
1171 IPRINTK("<vif handle=%u %s(%s) evtchn=%u tx=%p rx=%p>\n",
1172 np->handle,
1173 netif_carrier_ok(np->netdev) ? "on" : "off",
1174 netif_running(np->netdev) ? "open" : "closed",
1175 np->evtchn,
1176 np->tx,
1177 np->rx);
1178 } else
1179 IPRINTK("<vif NULL>\n");
1180 #endif
1183 static void netif_uninit(struct net_device *dev)
1185 struct netfront_info *np = netdev_priv(dev);
1186 gnttab_free_grant_references(np->gref_tx_head);
1187 gnttab_free_grant_references(np->gref_rx_head);
1190 static struct ethtool_ops network_ethtool_ops =
1192 .get_tx_csum = ethtool_op_get_tx_csum,
1193 .set_tx_csum = ethtool_op_set_tx_csum,
1194 .get_sg = ethtool_op_get_sg,
1195 .set_sg = xennet_set_sg,
1196 };
1198 #ifdef CONFIG_SYSFS
1199 static ssize_t show_rxbuf_min(struct class_device *cd, char *buf)
1201 struct net_device *netdev = container_of(cd, struct net_device,
1202 class_dev);
1203 struct netfront_info *info = netdev_priv(netdev);
1205 return sprintf(buf, "%u\n", info->rx_min_target);
1208 static ssize_t store_rxbuf_min(struct class_device *cd,
1209 const char *buf, size_t len)
1211 struct net_device *netdev = container_of(cd, struct net_device,
1212 class_dev);
1213 struct netfront_info *np = netdev_priv(netdev);
1214 char *endp;
1215 unsigned long target;
1217 if (!capable(CAP_NET_ADMIN))
1218 return -EPERM;
1220 target = simple_strtoul(buf, &endp, 0);
1221 if (endp == buf)
1222 return -EBADMSG;
1224 if (target < RX_MIN_TARGET)
1225 target = RX_MIN_TARGET;
1226 if (target > RX_MAX_TARGET)
1227 target = RX_MAX_TARGET;
1229 spin_lock(&np->rx_lock);
1230 if (target > np->rx_max_target)
1231 np->rx_max_target = target;
1232 np->rx_min_target = target;
1233 if (target > np->rx_target)
1234 np->rx_target = target;
1236 network_alloc_rx_buffers(netdev);
1238 spin_unlock(&np->rx_lock);
1239 return len;
1242 static ssize_t show_rxbuf_max(struct class_device *cd, char *buf)
1244 struct net_device *netdev = container_of(cd, struct net_device,
1245 class_dev);
1246 struct netfront_info *info = netdev_priv(netdev);
1248 return sprintf(buf, "%u\n", info->rx_max_target);
1251 static ssize_t store_rxbuf_max(struct class_device *cd,
1252 const char *buf, size_t len)
1254 struct net_device *netdev = container_of(cd, struct net_device,
1255 class_dev);
1256 struct netfront_info *np = netdev_priv(netdev);
1257 char *endp;
1258 unsigned long target;
1260 if (!capable(CAP_NET_ADMIN))
1261 return -EPERM;
1263 target = simple_strtoul(buf, &endp, 0);
1264 if (endp == buf)
1265 return -EBADMSG;
1267 if (target < RX_MIN_TARGET)
1268 target = RX_MIN_TARGET;
1269 if (target > RX_MAX_TARGET)
1270 target = RX_MAX_TARGET;
1272 spin_lock(&np->rx_lock);
1273 if (target < np->rx_min_target)
1274 np->rx_min_target = target;
1275 np->rx_max_target = target;
1276 if (target < np->rx_target)
1277 np->rx_target = target;
1279 network_alloc_rx_buffers(netdev);
1281 spin_unlock(&np->rx_lock);
1282 return len;
1285 static ssize_t show_rxbuf_cur(struct class_device *cd, char *buf)
1287 struct net_device *netdev = container_of(cd, struct net_device,
1288 class_dev);
1289 struct netfront_info *info = netdev_priv(netdev);
1291 return sprintf(buf, "%u\n", info->rx_target);
1294 static const struct class_device_attribute xennet_attrs[] = {
1295 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1296 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1297 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1298 };
1300 static int xennet_sysfs_addif(struct net_device *netdev)
1302 int i;
1303 int error = 0;
1305 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1306 error = class_device_create_file(&netdev->class_dev,
1307 &xennet_attrs[i]);
1308 if (error)
1309 goto fail;
1311 return 0;
1313 fail:
1314 while (--i >= 0)
1315 class_device_remove_file(&netdev->class_dev,
1316 &xennet_attrs[i]);
1317 return error;
1320 static void xennet_sysfs_delif(struct net_device *netdev)
1322 int i;
1324 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1325 class_device_remove_file(&netdev->class_dev,
1326 &xennet_attrs[i]);
1330 #endif /* CONFIG_SYSFS */
1333 /*
1334 * Nothing to do here. Virtual interface is point-to-point and the
1335 * physical interface is probably promiscuous anyway.
1336 */
1337 static void network_set_multicast_list(struct net_device *dev)
1341 /** Create a network device.
1342 * @param handle device handle
1343 * @param val return parameter for created device
1344 * @return 0 on success, error code otherwise
1345 */
1346 static struct net_device * __devinit create_netdev(int handle,
1347 struct xenbus_device *dev)
1349 int i, err = 0;
1350 struct net_device *netdev = NULL;
1351 struct netfront_info *np = NULL;
1353 netdev = alloc_etherdev(sizeof(struct netfront_info));
1354 if (!netdev) {
1355 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1356 __FUNCTION__);
1357 return ERR_PTR(-ENOMEM);
1360 np = netdev_priv(netdev);
1361 np->handle = handle;
1362 np->xbdev = dev;
1364 netif_carrier_off(netdev);
1366 spin_lock_init(&np->tx_lock);
1367 spin_lock_init(&np->rx_lock);
1369 skb_queue_head_init(&np->rx_batch);
1370 np->rx_target = RX_DFL_MIN_TARGET;
1371 np->rx_min_target = RX_DFL_MIN_TARGET;
1372 np->rx_max_target = RX_MAX_TARGET;
1374 init_timer(&np->rx_refill_timer);
1375 np->rx_refill_timer.data = (unsigned long)netdev;
1376 np->rx_refill_timer.function = rx_refill_timeout;
1378 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
1379 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1380 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1381 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1384 for (i = 0; i <= NET_RX_RING_SIZE; i++) {
1385 np->rx_skbs[i] = (void *)((unsigned long) i+1);
1386 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1389 /* A grant for every tx ring slot */
1390 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1391 &np->gref_tx_head) < 0) {
1392 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1393 err = -ENOMEM;
1394 goto exit;
1396 /* A grant for every rx ring slot */
1397 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1398 &np->gref_rx_head) < 0) {
1399 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1400 gnttab_free_grant_references(np->gref_tx_head);
1401 err = -ENOMEM;
1402 goto exit_free_tx;
1405 netdev->open = network_open;
1406 netdev->hard_start_xmit = network_start_xmit;
1407 netdev->stop = network_close;
1408 netdev->get_stats = network_get_stats;
1409 netdev->poll = netif_poll;
1410 netdev->set_multicast_list = network_set_multicast_list;
1411 netdev->uninit = netif_uninit;
1412 netdev->change_mtu = xennet_change_mtu;
1413 netdev->weight = 64;
1414 netdev->features = NETIF_F_IP_CSUM;
1416 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
1417 SET_MODULE_OWNER(netdev);
1418 SET_NETDEV_DEV(netdev, &dev->dev);
1420 err = register_netdev(netdev);
1421 if (err) {
1422 printk(KERN_WARNING "%s> register_netdev err=%d\n",
1423 __FUNCTION__, err);
1424 goto exit_free_rx;
1427 err = xennet_sysfs_addif(netdev);
1428 if (err) {
1429 /* This can be non-fatal: it only means no tuning parameters */
1430 printk(KERN_WARNING "%s> add sysfs failed err=%d\n",
1431 __FUNCTION__, err);
1434 np->netdev = netdev;
1436 return netdev;
1439 exit_free_rx:
1440 gnttab_free_grant_references(np->gref_rx_head);
1441 exit_free_tx:
1442 gnttab_free_grant_references(np->gref_tx_head);
1443 exit:
1444 free_netdev(netdev);
1445 return ERR_PTR(err);
1448 /*
1449 * We use this notifier to send out a fake ARP reply to reset switches and
1450 * router ARP caches when an IP interface is brought up on a VIF.
1451 */
1452 static int
1453 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1455 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1456 struct net_device *dev = ifa->ifa_dev->dev;
1458 /* UP event and is it one of our devices? */
1459 if (event == NETDEV_UP && dev->open == network_open)
1460 (void)send_fake_arp(dev);
1462 return NOTIFY_DONE;
1466 /* ** Close down ** */
1469 /**
1470 * Handle the change of state of the backend to Closing. We must delete our
1471 * device-layer structures now, to ensure that writes are flushed through to
1472 * the backend. Once is this done, we can switch to Closed in
1473 * acknowledgement.
1474 */
1475 static void netfront_closing(struct xenbus_device *dev)
1477 struct netfront_info *info = dev->data;
1479 DPRINTK("netfront_closing: %s removed\n", dev->nodename);
1481 close_netdev(info);
1483 xenbus_switch_state(dev, XenbusStateClosed);
1487 static int __devexit netfront_remove(struct xenbus_device *dev)
1489 struct netfront_info *info = dev->data;
1491 DPRINTK("%s\n", dev->nodename);
1493 netif_disconnect_backend(info);
1494 free_netdev(info->netdev);
1496 return 0;
1500 static void close_netdev(struct netfront_info *info)
1502 del_timer_sync(&info->rx_refill_timer);
1504 xennet_sysfs_delif(info->netdev);
1505 unregister_netdev(info->netdev);
1509 static void netif_disconnect_backend(struct netfront_info *info)
1511 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1512 spin_lock_irq(&info->tx_lock);
1513 spin_lock(&info->rx_lock);
1514 netif_carrier_off(info->netdev);
1515 spin_unlock(&info->rx_lock);
1516 spin_unlock_irq(&info->tx_lock);
1518 if (info->irq)
1519 unbind_from_irqhandler(info->irq, info->netdev);
1520 info->evtchn = info->irq = 0;
1522 end_access(info->tx_ring_ref, info->tx.sring);
1523 end_access(info->rx_ring_ref, info->rx.sring);
1524 info->tx_ring_ref = GRANT_INVALID_REF;
1525 info->rx_ring_ref = GRANT_INVALID_REF;
1526 info->tx.sring = NULL;
1527 info->rx.sring = NULL;
1531 static void netif_free(struct netfront_info *info)
1533 close_netdev(info);
1534 netif_disconnect_backend(info);
1535 free_netdev(info->netdev);
1539 static void end_access(int ref, void *page)
1541 if (ref != GRANT_INVALID_REF)
1542 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1546 /* ** Driver registration ** */
1549 static struct xenbus_device_id netfront_ids[] = {
1550 { "vif" },
1551 { "" }
1552 };
1555 static struct xenbus_driver netfront = {
1556 .name = "vif",
1557 .owner = THIS_MODULE,
1558 .ids = netfront_ids,
1559 .probe = netfront_probe,
1560 .remove = __devexit_p(netfront_remove),
1561 .resume = netfront_resume,
1562 .otherend_changed = backend_changed,
1563 };
1566 static struct notifier_block notifier_inetdev = {
1567 .notifier_call = inetdev_notify,
1568 .next = NULL,
1569 .priority = 0
1570 };
1572 static int __init netif_init(void)
1574 if (!is_running_on_xen())
1575 return -ENODEV;
1577 if (xen_start_info->flags & SIF_INITDOMAIN)
1578 return 0;
1580 IPRINTK("Initialising virtual ethernet driver.\n");
1582 (void)register_inetaddr_notifier(&notifier_inetdev);
1584 return xenbus_register_frontend(&netfront);
1586 module_init(netif_init);
1589 static void __exit netif_exit(void)
1591 unregister_inetaddr_notifier(&notifier_inetdev);
1593 return xenbus_unregister_driver(&netfront);
1595 module_exit(netif_exit);
1597 MODULE_LICENSE("Dual BSD/GPL");