ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 10827:3c95a8ed22d0

[NET] front: Zero gso features/pad

Set unused gso features/pad values to zero for forward-compatibility.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
author kfraser@localhost.localdomain
date Thu Jul 27 13:32:29 2006 +0100 (2006-07-27)
parents b066b4612286
children 03c8002068d9
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...) \
142 pr_debug("netfront (%s:%d) " fmt, \
143 __FUNCTION__, __LINE__, ##args)
144 #define IPRINTK(fmt, args...) \
145 printk(KERN_INFO "netfront: " fmt, ##args)
146 #define WPRINTK(fmt, args...) \
147 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 network_connect(struct net_device *);
161 static void network_tx_buf_gc(struct net_device *);
162 static void network_alloc_rx_buffers(struct net_device *);
163 static int send_fake_arp(struct net_device *);
165 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
167 #ifdef CONFIG_SYSFS
168 static int xennet_sysfs_addif(struct net_device *netdev);
169 static void xennet_sysfs_delif(struct net_device *netdev);
170 #else /* !CONFIG_SYSFS */
171 #define xennet_sysfs_addif(dev) (0)
172 #define xennet_sysfs_delif(dev) do { } while(0)
173 #endif
175 static inline int xennet_can_sg(struct net_device *dev)
176 {
177 return dev->features & NETIF_F_SG;
178 }
180 /**
181 * Entry point to this code when a new device is created. Allocate the basic
182 * structures and the ring buffers for communication with the backend, and
183 * inform the backend of the appropriate details for those. Switch to
184 * Connected state.
185 */
186 static int __devinit netfront_probe(struct xenbus_device *dev,
187 const struct xenbus_device_id *id)
188 {
189 int err;
190 struct net_device *netdev;
191 struct netfront_info *info;
192 unsigned int handle;
194 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%u", &handle);
195 if (err != 1) {
196 xenbus_dev_fatal(dev, err, "reading handle");
197 return err;
198 }
200 netdev = create_netdev(handle, dev);
201 if (IS_ERR(netdev)) {
202 err = PTR_ERR(netdev);
203 xenbus_dev_fatal(dev, err, "creating netdev");
204 return err;
205 }
207 info = netdev_priv(netdev);
208 dev->dev.driver_data = info;
210 err = talk_to_backend(dev, info);
211 if (err) {
212 xennet_sysfs_delif(info->netdev);
213 unregister_netdev(netdev);
214 free_netdev(netdev);
215 dev->dev.driver_data = NULL;
216 return err;
217 }
219 return 0;
220 }
223 /**
224 * We are reconnecting to the backend, due to a suspend/resume, or a backend
225 * driver restart. We tear down our netif structure and recreate it, but
226 * leave the device-layer structures intact so that this is transparent to the
227 * rest of the kernel.
228 */
229 static int netfront_resume(struct xenbus_device *dev)
230 {
231 struct netfront_info *info = dev->dev.driver_data;
233 DPRINTK("%s\n", dev->nodename);
235 netif_disconnect_backend(info);
236 return talk_to_backend(dev, info);
237 }
239 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
240 {
241 char *s, *e, *macstr;
242 int i;
244 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
245 if (IS_ERR(macstr))
246 return PTR_ERR(macstr);
248 for (i = 0; i < ETH_ALEN; i++) {
249 mac[i] = simple_strtoul(s, &e, 16);
250 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
251 kfree(macstr);
252 return -ENOENT;
253 }
254 s = e+1;
255 }
257 kfree(macstr);
258 return 0;
259 }
261 /* Common code used when first setting up, and when resuming. */
262 static int talk_to_backend(struct xenbus_device *dev,
263 struct netfront_info *info)
264 {
265 const char *message;
266 struct xenbus_transaction xbt;
267 int err;
269 err = xen_net_read_mac(dev, info->mac);
270 if (err) {
271 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
272 goto out;
273 }
275 /* Create shared ring, alloc event channel. */
276 err = setup_device(dev, info);
277 if (err)
278 goto out;
280 again:
281 err = xenbus_transaction_start(&xbt);
282 if (err) {
283 xenbus_dev_fatal(dev, err, "starting transaction");
284 goto destroy_ring;
285 }
287 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
288 info->tx_ring_ref);
289 if (err) {
290 message = "writing tx ring-ref";
291 goto abort_transaction;
292 }
293 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
294 info->rx_ring_ref);
295 if (err) {
296 message = "writing rx ring-ref";
297 goto abort_transaction;
298 }
299 err = xenbus_printf(xbt, dev->nodename,
300 "event-channel", "%u", info->evtchn);
301 if (err) {
302 message = "writing event-channel";
303 goto abort_transaction;
304 }
306 err = xenbus_transaction_end(xbt, 0);
307 if (err) {
308 if (err == -EAGAIN)
309 goto again;
310 xenbus_dev_fatal(dev, err, "completing transaction");
311 goto destroy_ring;
312 }
314 return 0;
316 abort_transaction:
317 xenbus_transaction_end(xbt, 1);
318 xenbus_dev_fatal(dev, err, "%s", message);
319 destroy_ring:
320 netif_free(info);
321 out:
322 return err;
323 }
326 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
327 {
328 struct netif_tx_sring *txs;
329 struct netif_rx_sring *rxs;
330 int err;
331 struct net_device *netdev = info->netdev;
333 info->tx_ring_ref = GRANT_INVALID_REF;
334 info->rx_ring_ref = GRANT_INVALID_REF;
335 info->rx.sring = NULL;
336 info->tx.sring = NULL;
337 info->irq = 0;
339 txs = (struct netif_tx_sring *)get_zeroed_page(GFP_KERNEL);
340 if (!txs) {
341 err = -ENOMEM;
342 xenbus_dev_fatal(dev, err, "allocating tx ring page");
343 goto fail;
344 }
345 SHARED_RING_INIT(txs);
346 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
348 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
349 if (err < 0) {
350 free_page((unsigned long)txs);
351 goto fail;
352 }
353 info->tx_ring_ref = err;
355 rxs = (struct netif_rx_sring *)get_zeroed_page(GFP_KERNEL);
356 if (!rxs) {
357 err = -ENOMEM;
358 xenbus_dev_fatal(dev, err, "allocating rx ring page");
359 goto fail;
360 }
361 SHARED_RING_INIT(rxs);
362 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
364 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
365 if (err < 0) {
366 free_page((unsigned long)rxs);
367 goto fail;
368 }
369 info->rx_ring_ref = err;
371 err = xenbus_alloc_evtchn(dev, &info->evtchn);
372 if (err)
373 goto fail;
375 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
376 err = bind_evtchn_to_irqhandler(info->evtchn, netif_int,
377 SA_SAMPLE_RANDOM, netdev->name, netdev);
378 if (err < 0)
379 goto fail;
380 info->irq = err;
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->dev.driver_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 break;
414 case XenbusStateClosing:
415 netfront_closing(dev);
416 break;
417 }
418 }
421 /** Send a packet on a net device to encourage switches to learn the
422 * MAC. We send a fake ARP request.
423 *
424 * @param dev device
425 * @return 0 on success, error code otherwise
426 */
427 static int send_fake_arp(struct net_device *dev)
428 {
429 struct sk_buff *skb;
430 u32 src_ip, dst_ip;
432 dst_ip = INADDR_BROADCAST;
433 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
435 /* No IP? Then nothing to do. */
436 if (src_ip == 0)
437 return 0;
439 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
440 dst_ip, dev, src_ip,
441 /*dst_hw*/ NULL, /*src_hw*/ NULL,
442 /*target_hw*/ dev->dev_addr);
443 if (skb == NULL)
444 return -ENOMEM;
446 return dev_queue_xmit(skb);
447 }
450 static int network_open(struct net_device *dev)
451 {
452 struct netfront_info *np = netdev_priv(dev);
454 memset(&np->stats, 0, sizeof(np->stats));
456 network_alloc_rx_buffers(dev);
457 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
459 netif_start_queue(dev);
461 return 0;
462 }
464 static inline int netfront_tx_slot_available(struct netfront_info *np)
465 {
466 return RING_FREE_REQUESTS(&np->tx) >= MAX_SKB_FRAGS + 2;
467 }
469 static inline void network_maybe_wake_tx(struct net_device *dev)
470 {
471 struct netfront_info *np = netdev_priv(dev);
473 if (unlikely(netif_queue_stopped(dev)) &&
474 netfront_tx_slot_available(np) &&
475 likely(netif_running(dev)))
476 netif_wake_queue(dev);
477 }
479 static void network_tx_buf_gc(struct net_device *dev)
480 {
481 RING_IDX cons, prod;
482 unsigned short id;
483 struct netfront_info *np = netdev_priv(dev);
484 struct sk_buff *skb;
486 if (unlikely(!netif_carrier_ok(dev)))
487 return;
489 do {
490 prod = np->tx.sring->rsp_prod;
491 rmb(); /* Ensure we see responses up to 'rp'. */
493 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
494 struct netif_tx_response *txrsp;
496 txrsp = RING_GET_RESPONSE(&np->tx, cons);
497 if (txrsp->status == NETIF_RSP_NULL)
498 continue;
500 id = txrsp->id;
501 skb = np->tx_skbs[id];
502 if (unlikely(gnttab_query_foreign_access(
503 np->grant_tx_ref[id]) != 0)) {
504 printk(KERN_ALERT "network_tx_buf_gc: warning "
505 "-- grant still in use by backend "
506 "domain.\n");
507 BUG();
508 }
509 gnttab_end_foreign_access_ref(
510 np->grant_tx_ref[id], GNTMAP_readonly);
511 gnttab_release_grant_reference(
512 &np->gref_tx_head, np->grant_tx_ref[id]);
513 np->grant_tx_ref[id] = GRANT_INVALID_REF;
514 add_id_to_freelist(np->tx_skbs, id);
515 dev_kfree_skb_irq(skb);
516 }
518 np->tx.rsp_cons = prod;
520 /*
521 * Set a new event, then check for race with update of tx_cons.
522 * Note that it is essential to schedule a callback, no matter
523 * how few buffers are pending. Even if there is space in the
524 * transmit ring, higher layers may be blocked because too much
525 * data is outstanding: in such cases notification from Xen is
526 * likely to be the only kick that we'll get.
527 */
528 np->tx.sring->rsp_event =
529 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
530 mb();
531 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
533 network_maybe_wake_tx(dev);
534 }
537 static void rx_refill_timeout(unsigned long data)
538 {
539 struct net_device *dev = (struct net_device *)data;
540 netif_rx_schedule(dev);
541 }
544 static void network_alloc_rx_buffers(struct net_device *dev)
545 {
546 unsigned short id;
547 struct netfront_info *np = netdev_priv(dev);
548 struct sk_buff *skb;
549 int i, batch_target;
550 RING_IDX req_prod = np->rx.req_prod_pvt;
551 struct xen_memory_reservation reservation;
552 grant_ref_t ref;
554 if (unlikely(!netif_carrier_ok(dev)))
555 return;
557 /*
558 * Allocate skbuffs greedily, even though we batch updates to the
559 * receive ring. This creates a less bursty demand on the memory
560 * allocator, so should reduce the chance of failed allocation requests
561 * both for ourself and for other kernel subsystems.
562 */
563 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
564 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
565 /*
566 * Subtract dev_alloc_skb headroom (16 bytes) and shared info
567 * tailroom then round down to SKB_DATA_ALIGN boundary.
568 */
569 skb = __dev_alloc_skb(
570 ((PAGE_SIZE - sizeof(struct skb_shared_info)) &
571 (-SKB_DATA_ALIGN(1))) - 16,
572 GFP_ATOMIC|__GFP_NOWARN);
573 if (skb == NULL) {
574 /* Any skbuffs queued for refill? Force them out. */
575 if (i != 0)
576 goto refill;
577 /* Could not allocate any skbuffs. Try again later. */
578 mod_timer(&np->rx_refill_timer,
579 jiffies + (HZ/10));
580 return;
581 }
582 __skb_queue_tail(&np->rx_batch, skb);
583 }
585 /* Is the batch large enough to be worthwhile? */
586 if (i < (np->rx_target/2))
587 return;
589 /* Adjust our fill target if we risked running out of buffers. */
590 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
591 ((np->rx_target *= 2) > np->rx_max_target))
592 np->rx_target = np->rx_max_target;
594 refill:
595 for (i = 0; ; i++) {
596 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
597 break;
599 skb->dev = dev;
601 id = get_id_from_freelist(np->rx_skbs);
603 np->rx_skbs[id] = skb;
605 RING_GET_REQUEST(&np->rx, req_prod + i)->id = id;
606 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
607 BUG_ON((signed short)ref < 0);
608 np->grant_rx_ref[id] = ref;
609 gnttab_grant_foreign_transfer_ref(ref,
610 np->xbdev->otherend_id,
611 __pa(skb->head)>>PAGE_SHIFT);
612 RING_GET_REQUEST(&np->rx, req_prod + i)->gref = ref;
613 np->rx_pfn_array[i] = virt_to_mfn(skb->head);
615 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
616 /* Remove this page before passing back to Xen. */
617 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
618 INVALID_P2M_ENTRY);
619 MULTI_update_va_mapping(np->rx_mcl+i,
620 (unsigned long)skb->head,
621 __pte(0), 0);
622 }
623 }
625 /* Tell the ballon driver what is going on. */
626 balloon_update_driver_allowance(i);
628 set_xen_guest_handle(reservation.extent_start, np->rx_pfn_array);
629 reservation.nr_extents = i;
630 reservation.extent_order = 0;
631 reservation.address_bits = 0;
632 reservation.domid = DOMID_SELF;
634 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
635 /* After all PTEs have been zapped, flush the TLB. */
636 np->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
637 UVMF_TLB_FLUSH|UVMF_ALL;
639 /* Give away a batch of pages. */
640 np->rx_mcl[i].op = __HYPERVISOR_memory_op;
641 np->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
642 np->rx_mcl[i].args[1] = (unsigned long)&reservation;
644 /* Zap PTEs and give away pages in one big multicall. */
645 (void)HYPERVISOR_multicall(np->rx_mcl, i+1);
647 /* Check return status of HYPERVISOR_memory_op(). */
648 if (unlikely(np->rx_mcl[i].result != i))
649 panic("Unable to reduce memory reservation\n");
650 } else
651 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
652 &reservation) != i)
653 panic("Unable to reduce memory reservation\n");
655 /* Above is a suitable barrier to ensure backend will see requests. */
656 np->rx.req_prod_pvt = req_prod + i;
657 RING_PUSH_REQUESTS(&np->rx);
658 }
660 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
661 struct netif_tx_request *tx)
662 {
663 struct netfront_info *np = netdev_priv(dev);
664 char *data = skb->data;
665 unsigned long mfn;
666 RING_IDX prod = np->tx.req_prod_pvt;
667 int frags = skb_shinfo(skb)->nr_frags;
668 unsigned int offset = offset_in_page(data);
669 unsigned int len = skb_headlen(skb);
670 unsigned int id;
671 grant_ref_t ref;
672 int i;
674 while (len > PAGE_SIZE - offset) {
675 tx->size = PAGE_SIZE - offset;
676 tx->flags |= NETTXF_more_data;
677 len -= tx->size;
678 data += tx->size;
679 offset = 0;
681 id = get_id_from_freelist(np->tx_skbs);
682 np->tx_skbs[id] = skb_get(skb);
683 tx = RING_GET_REQUEST(&np->tx, prod++);
684 tx->id = id;
685 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
686 BUG_ON((signed short)ref < 0);
688 mfn = virt_to_mfn(data);
689 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
690 mfn, GNTMAP_readonly);
692 tx->gref = np->grant_tx_ref[id] = ref;
693 tx->offset = offset;
694 tx->size = len;
695 tx->flags = 0;
696 }
698 for (i = 0; i < frags; i++) {
699 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
701 tx->flags |= NETTXF_more_data;
703 id = get_id_from_freelist(np->tx_skbs);
704 np->tx_skbs[id] = skb_get(skb);
705 tx = RING_GET_REQUEST(&np->tx, prod++);
706 tx->id = id;
707 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
708 BUG_ON((signed short)ref < 0);
710 mfn = pfn_to_mfn(page_to_pfn(frag->page));
711 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
712 mfn, GNTMAP_readonly);
714 tx->gref = np->grant_tx_ref[id] = ref;
715 tx->offset = frag->page_offset;
716 tx->size = frag->size;
717 tx->flags = 0;
718 }
720 np->tx.req_prod_pvt = prod;
721 }
723 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
724 {
725 unsigned short id;
726 struct netfront_info *np = netdev_priv(dev);
727 struct netif_tx_request *tx;
728 struct netif_extra_info *extra;
729 char *data = skb->data;
730 RING_IDX i;
731 grant_ref_t ref;
732 unsigned long mfn;
733 int notify;
734 int frags = skb_shinfo(skb)->nr_frags;
735 unsigned int offset = offset_in_page(data);
736 unsigned int len = skb_headlen(skb);
738 frags += (offset + len + PAGE_SIZE - 1) / PAGE_SIZE;
739 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
740 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
741 frags);
742 dump_stack();
743 goto drop;
744 }
746 spin_lock_irq(&np->tx_lock);
748 if (unlikely(!netif_carrier_ok(dev) ||
749 (frags > 1 && !xennet_can_sg(dev)) ||
750 netif_needs_gso(dev, skb))) {
751 spin_unlock_irq(&np->tx_lock);
752 goto drop;
753 }
755 i = np->tx.req_prod_pvt;
757 id = get_id_from_freelist(np->tx_skbs);
758 np->tx_skbs[id] = skb;
760 tx = RING_GET_REQUEST(&np->tx, i);
762 tx->id = id;
763 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
764 BUG_ON((signed short)ref < 0);
765 mfn = virt_to_mfn(data);
766 gnttab_grant_foreign_access_ref(
767 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
768 tx->gref = np->grant_tx_ref[id] = ref;
769 tx->offset = offset;
770 tx->size = len;
772 tx->flags = 0;
773 extra = NULL;
775 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
776 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
777 if (skb->proto_data_valid) /* remote but checksummed? */
778 tx->flags |= NETTXF_data_validated;
780 if (skb_shinfo(skb)->gso_size) {
781 struct netif_extra_info *gso = (struct netif_extra_info *)
782 RING_GET_REQUEST(&np->tx, ++i);
784 if (extra)
785 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
786 else
787 tx->flags |= NETTXF_extra_info;
789 gso->u.gso.size = skb_shinfo(skb)->gso_size;
790 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
791 gso->u.gso.pad = 0;
792 gso->u.gso.features = 0;
794 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
795 gso->flags = 0;
796 extra = gso;
797 }
799 np->tx.req_prod_pvt = i + 1;
801 xennet_make_frags(skb, dev, tx);
802 tx->size = skb->len;
804 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
805 if (notify)
806 notify_remote_via_irq(np->irq);
808 network_tx_buf_gc(dev);
810 if (!netfront_tx_slot_available(np))
811 netif_stop_queue(dev);
813 spin_unlock_irq(&np->tx_lock);
815 np->stats.tx_bytes += skb->len;
816 np->stats.tx_packets++;
818 return 0;
820 drop:
821 np->stats.tx_dropped++;
822 dev_kfree_skb(skb);
823 return 0;
824 }
826 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
827 {
828 struct net_device *dev = dev_id;
829 struct netfront_info *np = netdev_priv(dev);
830 unsigned long flags;
832 spin_lock_irqsave(&np->tx_lock, flags);
833 network_tx_buf_gc(dev);
834 spin_unlock_irqrestore(&np->tx_lock, flags);
836 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx) &&
837 likely(netif_running(dev)))
838 netif_rx_schedule(dev);
840 return IRQ_HANDLED;
841 }
844 static int netif_poll(struct net_device *dev, int *pbudget)
845 {
846 struct netfront_info *np = netdev_priv(dev);
847 struct sk_buff *skb, *nskb;
848 struct netif_rx_response *rx;
849 RING_IDX i, rp;
850 struct mmu_update *mmu = np->rx_mmu;
851 struct multicall_entry *mcl = np->rx_mcl;
852 int work_done, budget, more_to_do = 1;
853 struct sk_buff_head rxq;
854 unsigned long flags;
855 unsigned long mfn;
856 grant_ref_t ref;
858 spin_lock(&np->rx_lock);
860 if (unlikely(!netif_carrier_ok(dev))) {
861 spin_unlock(&np->rx_lock);
862 return 0;
863 }
865 skb_queue_head_init(&rxq);
867 if ((budget = *pbudget) > dev->quota)
868 budget = dev->quota;
869 rp = np->rx.sring->rsp_prod;
870 rmb(); /* Ensure we see queued responses up to 'rp'. */
872 for (i = np->rx.rsp_cons, work_done = 0;
873 (i != rp) && (work_done < budget);
874 i++, work_done++) {
875 rx = RING_GET_RESPONSE(&np->rx, i);
877 /*
878 * This definitely indicates a bug, either in this driver or in
879 * the backend driver. In future this should flag the bad
880 * situation to the system controller to reboot the backed.
881 */
882 if ((ref = np->grant_rx_ref[rx->id]) == GRANT_INVALID_REF) {
883 WPRINTK("Bad rx response id %d.\n", rx->id);
884 work_done--;
885 continue;
886 }
888 /* Memory pressure, insufficient buffer headroom, ... */
889 if ((mfn = gnttab_end_foreign_transfer_ref(ref)) == 0) {
890 if (net_ratelimit())
891 WPRINTK("Unfulfilled rx req (id=%d, st=%d).\n",
892 rx->id, rx->status);
893 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id =
894 rx->id;
895 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref =
896 ref;
897 np->rx.req_prod_pvt++;
898 RING_PUSH_REQUESTS(&np->rx);
899 work_done--;
900 continue;
901 }
903 gnttab_release_grant_reference(&np->gref_rx_head, ref);
904 np->grant_rx_ref[rx->id] = GRANT_INVALID_REF;
906 skb = np->rx_skbs[rx->id];
907 add_id_to_freelist(np->rx_skbs, rx->id);
909 /* NB. We handle skb overflow later. */
910 skb->data = skb->head + rx->offset;
911 skb->len = rx->status;
912 skb->tail = skb->data + skb->len;
914 /*
915 * Old backends do not assert data_validated but we
916 * can infer it from csum_blank so test both flags.
917 */
918 if (rx->flags & (NETRXF_data_validated|NETRXF_csum_blank)) {
919 skb->ip_summed = CHECKSUM_UNNECESSARY;
920 skb->proto_data_valid = 1;
921 } else {
922 skb->ip_summed = CHECKSUM_NONE;
923 skb->proto_data_valid = 0;
924 }
925 skb->proto_csum_blank = !!(rx->flags & NETRXF_csum_blank);
927 np->stats.rx_packets++;
928 np->stats.rx_bytes += rx->status;
930 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
931 /* Remap the page. */
932 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
933 pfn_pte_ma(mfn, PAGE_KERNEL),
934 0);
935 mcl++;
936 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
937 | MMU_MACHPHYS_UPDATE;
938 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
939 mmu++;
941 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
942 mfn);
943 }
945 __skb_queue_tail(&rxq, skb);
946 }
948 /* Some pages are no longer absent... */
949 balloon_update_driver_allowance(-work_done);
951 /* Do all the remapping work, and M2P updates, in one big hypercall. */
952 if (likely((mcl - np->rx_mcl) != 0)) {
953 mcl->op = __HYPERVISOR_mmu_update;
954 mcl->args[0] = (unsigned long)np->rx_mmu;
955 mcl->args[1] = mmu - np->rx_mmu;
956 mcl->args[2] = 0;
957 mcl->args[3] = DOMID_SELF;
958 mcl++;
959 (void)HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
960 }
962 while ((skb = __skb_dequeue(&rxq)) != NULL) {
963 if (skb->len > (dev->mtu + ETH_HLEN + 4)) {
964 if (net_ratelimit())
965 printk(KERN_INFO "Received packet too big for "
966 "MTU (%d > %d)\n",
967 skb->len - ETH_HLEN - 4, dev->mtu);
968 skb->len = 0;
969 skb->tail = skb->data;
970 init_skb_shinfo(skb);
971 dev_kfree_skb(skb);
972 continue;
973 }
975 /*
976 * Enough room in skbuff for the data we were passed? Also,
977 * Linux expects at least 16 bytes headroom in each rx buffer.
978 */
979 if (unlikely(skb->tail > skb->end) ||
980 unlikely((skb->data - skb->head) < 16)) {
981 if (net_ratelimit()) {
982 if (skb->tail > skb->end)
983 printk(KERN_INFO "Received packet "
984 "is %zd bytes beyond tail.\n",
985 skb->tail - skb->end);
986 else
987 printk(KERN_INFO "Received packet "
988 "is %zd bytes before head.\n",
989 16 - (skb->data - skb->head));
990 }
992 nskb = __dev_alloc_skb(skb->len + 2,
993 GFP_ATOMIC|__GFP_NOWARN);
994 if (nskb != NULL) {
995 skb_reserve(nskb, 2);
996 skb_put(nskb, skb->len);
997 memcpy(nskb->data, skb->data, skb->len);
998 /* Copy any other fields we already set up. */
999 nskb->dev = skb->dev;
1000 nskb->ip_summed = skb->ip_summed;
1001 nskb->proto_data_valid = skb->proto_data_valid;
1002 nskb->proto_csum_blank = skb->proto_csum_blank;
1005 /* Reinitialise and then destroy the old skbuff. */
1006 skb->len = 0;
1007 skb->tail = skb->data;
1008 init_skb_shinfo(skb);
1009 dev_kfree_skb(skb);
1011 /* Switch old for new, if we copied the buffer. */
1012 if ((skb = nskb) == NULL)
1013 continue;
1016 /* Set the shinfo area, which is hidden behind the data. */
1017 init_skb_shinfo(skb);
1018 /* Ethernet work: Delayed to here as it peeks the header. */
1019 skb->protocol = eth_type_trans(skb, dev);
1021 /* Pass it up. */
1022 netif_receive_skb(skb);
1023 dev->last_rx = jiffies;
1026 np->rx.rsp_cons = i;
1028 /* If we get a callback with very few responses, reduce fill target. */
1029 /* NB. Note exponential increase, linear decrease. */
1030 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
1031 ((3*np->rx_target) / 4)) &&
1032 (--np->rx_target < np->rx_min_target))
1033 np->rx_target = np->rx_min_target;
1035 network_alloc_rx_buffers(dev);
1037 *pbudget -= work_done;
1038 dev->quota -= work_done;
1040 if (work_done < budget) {
1041 local_irq_save(flags);
1043 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
1044 if (!more_to_do)
1045 __netif_rx_complete(dev);
1047 local_irq_restore(flags);
1050 spin_unlock(&np->rx_lock);
1052 return more_to_do;
1056 static int network_close(struct net_device *dev)
1058 struct netfront_info *np = netdev_priv(dev);
1059 netif_stop_queue(np->netdev);
1060 return 0;
1064 static struct net_device_stats *network_get_stats(struct net_device *dev)
1066 struct netfront_info *np = netdev_priv(dev);
1067 return &np->stats;
1070 static int xennet_change_mtu(struct net_device *dev, int mtu)
1072 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
1074 if (mtu > max)
1075 return -EINVAL;
1076 dev->mtu = mtu;
1077 return 0;
1080 static int xennet_set_sg(struct net_device *dev, u32 data)
1082 if (data) {
1083 struct netfront_info *np = netdev_priv(dev);
1084 int val;
1086 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1087 "%d", &val) < 0)
1088 val = 0;
1089 if (!val)
1090 return -ENOSYS;
1091 } else if (dev->mtu > ETH_DATA_LEN)
1092 dev->mtu = ETH_DATA_LEN;
1094 return ethtool_op_set_sg(dev, data);
1097 static int xennet_set_tso(struct net_device *dev, u32 data)
1099 if (data) {
1100 struct netfront_info *np = netdev_priv(dev);
1101 int val;
1103 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1104 "feature-gso-tcpv4", "%d", &val) < 0)
1105 val = 0;
1106 #if 0 /* KAF: After the protocol is finalised. */
1107 if (!val)
1108 #endif
1109 return -ENOSYS;
1112 return ethtool_op_set_tso(dev, data);
1115 static void xennet_set_features(struct net_device *dev)
1117 /* Turn off all GSO bits except ROBUST. */
1118 dev->features &= (1 << NETIF_F_GSO_SHIFT) - 1;
1119 dev->features |= NETIF_F_GSO_ROBUST;
1120 xennet_set_sg(dev, 0);
1122 if (!xennet_set_sg(dev, 1))
1123 xennet_set_tso(dev, 1);
1126 static void network_connect(struct net_device *dev)
1128 struct netfront_info *np = netdev_priv(dev);
1129 int i, requeue_idx;
1130 struct sk_buff *skb;
1132 xennet_set_features(dev);
1134 spin_lock_irq(&np->tx_lock);
1135 spin_lock(&np->rx_lock);
1137 /*
1138 * Recovery procedure:
1139 * NB. Freelist index entries are always going to be less than
1140 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
1141 * greater than PAGE_OFFSET: we use this property to distinguish
1142 * them.
1143 */
1145 /* Step 1: Discard all pending TX packet fragments. */
1146 for (requeue_idx = 0, i = 1; i <= NET_TX_RING_SIZE; i++) {
1147 if ((unsigned long)np->tx_skbs[i] < PAGE_OFFSET)
1148 continue;
1150 skb = np->tx_skbs[i];
1151 gnttab_end_foreign_access_ref(
1152 np->grant_tx_ref[i], GNTMAP_readonly);
1153 gnttab_release_grant_reference(
1154 &np->gref_tx_head, np->grant_tx_ref[i]);
1155 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1156 add_id_to_freelist(np->tx_skbs, i);
1157 dev_kfree_skb_irq(skb);
1160 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1161 for (requeue_idx = 0, i = 1; i <= NET_RX_RING_SIZE; i++) {
1162 if ((unsigned long)np->rx_skbs[i] < PAGE_OFFSET)
1163 continue;
1164 gnttab_grant_foreign_transfer_ref(
1165 np->grant_rx_ref[i], np->xbdev->otherend_id,
1166 __pa(np->rx_skbs[i]->data) >> PAGE_SHIFT);
1167 RING_GET_REQUEST(&np->rx, requeue_idx)->gref =
1168 np->grant_rx_ref[i];
1169 RING_GET_REQUEST(&np->rx, requeue_idx)->id = i;
1170 requeue_idx++;
1173 np->rx.req_prod_pvt = requeue_idx;
1174 RING_PUSH_REQUESTS(&np->rx);
1176 /*
1177 * Step 3: All public and private state should now be sane. Get
1178 * ready to start sending and receiving packets and give the driver
1179 * domain a kick because we've probably just requeued some
1180 * packets.
1181 */
1182 netif_carrier_on(dev);
1183 notify_remote_via_irq(np->irq);
1184 network_tx_buf_gc(dev);
1185 network_alloc_rx_buffers(dev);
1187 spin_unlock(&np->rx_lock);
1188 spin_unlock_irq(&np->tx_lock);
1191 static void netif_uninit(struct net_device *dev)
1193 struct netfront_info *np = netdev_priv(dev);
1194 gnttab_free_grant_references(np->gref_tx_head);
1195 gnttab_free_grant_references(np->gref_rx_head);
1198 static struct ethtool_ops network_ethtool_ops =
1200 .get_tx_csum = ethtool_op_get_tx_csum,
1201 .set_tx_csum = ethtool_op_set_tx_csum,
1202 .get_sg = ethtool_op_get_sg,
1203 .set_sg = xennet_set_sg,
1204 .get_tso = ethtool_op_get_tso,
1205 .set_tso = xennet_set_tso,
1206 .get_link = ethtool_op_get_link,
1207 };
1209 #ifdef CONFIG_SYSFS
1210 static ssize_t show_rxbuf_min(struct class_device *cd, char *buf)
1212 struct net_device *netdev = container_of(cd, struct net_device,
1213 class_dev);
1214 struct netfront_info *info = netdev_priv(netdev);
1216 return sprintf(buf, "%u\n", info->rx_min_target);
1219 static ssize_t store_rxbuf_min(struct class_device *cd,
1220 const char *buf, size_t len)
1222 struct net_device *netdev = container_of(cd, struct net_device,
1223 class_dev);
1224 struct netfront_info *np = netdev_priv(netdev);
1225 char *endp;
1226 unsigned long target;
1228 if (!capable(CAP_NET_ADMIN))
1229 return -EPERM;
1231 target = simple_strtoul(buf, &endp, 0);
1232 if (endp == buf)
1233 return -EBADMSG;
1235 if (target < RX_MIN_TARGET)
1236 target = RX_MIN_TARGET;
1237 if (target > RX_MAX_TARGET)
1238 target = RX_MAX_TARGET;
1240 spin_lock(&np->rx_lock);
1241 if (target > np->rx_max_target)
1242 np->rx_max_target = target;
1243 np->rx_min_target = target;
1244 if (target > np->rx_target)
1245 np->rx_target = target;
1247 network_alloc_rx_buffers(netdev);
1249 spin_unlock(&np->rx_lock);
1250 return len;
1253 static ssize_t show_rxbuf_max(struct class_device *cd, char *buf)
1255 struct net_device *netdev = container_of(cd, struct net_device,
1256 class_dev);
1257 struct netfront_info *info = netdev_priv(netdev);
1259 return sprintf(buf, "%u\n", info->rx_max_target);
1262 static ssize_t store_rxbuf_max(struct class_device *cd,
1263 const char *buf, size_t len)
1265 struct net_device *netdev = container_of(cd, struct net_device,
1266 class_dev);
1267 struct netfront_info *np = netdev_priv(netdev);
1268 char *endp;
1269 unsigned long target;
1271 if (!capable(CAP_NET_ADMIN))
1272 return -EPERM;
1274 target = simple_strtoul(buf, &endp, 0);
1275 if (endp == buf)
1276 return -EBADMSG;
1278 if (target < RX_MIN_TARGET)
1279 target = RX_MIN_TARGET;
1280 if (target > RX_MAX_TARGET)
1281 target = RX_MAX_TARGET;
1283 spin_lock(&np->rx_lock);
1284 if (target < np->rx_min_target)
1285 np->rx_min_target = target;
1286 np->rx_max_target = target;
1287 if (target < np->rx_target)
1288 np->rx_target = target;
1290 network_alloc_rx_buffers(netdev);
1292 spin_unlock(&np->rx_lock);
1293 return len;
1296 static ssize_t show_rxbuf_cur(struct class_device *cd, char *buf)
1298 struct net_device *netdev = container_of(cd, struct net_device,
1299 class_dev);
1300 struct netfront_info *info = netdev_priv(netdev);
1302 return sprintf(buf, "%u\n", info->rx_target);
1305 static const struct class_device_attribute xennet_attrs[] = {
1306 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1307 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1308 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1309 };
1311 static int xennet_sysfs_addif(struct net_device *netdev)
1313 int i;
1314 int error = 0;
1316 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1317 error = class_device_create_file(&netdev->class_dev,
1318 &xennet_attrs[i]);
1319 if (error)
1320 goto fail;
1322 return 0;
1324 fail:
1325 while (--i >= 0)
1326 class_device_remove_file(&netdev->class_dev,
1327 &xennet_attrs[i]);
1328 return error;
1331 static void xennet_sysfs_delif(struct net_device *netdev)
1333 int i;
1335 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1336 class_device_remove_file(&netdev->class_dev,
1337 &xennet_attrs[i]);
1341 #endif /* CONFIG_SYSFS */
1344 /*
1345 * Nothing to do here. Virtual interface is point-to-point and the
1346 * physical interface is probably promiscuous anyway.
1347 */
1348 static void network_set_multicast_list(struct net_device *dev)
1352 /** Create a network device.
1353 * @param handle device handle
1354 * @param val return parameter for created device
1355 * @return 0 on success, error code otherwise
1356 */
1357 static struct net_device * __devinit create_netdev(int handle,
1358 struct xenbus_device *dev)
1360 int i, err = 0;
1361 struct net_device *netdev = NULL;
1362 struct netfront_info *np = NULL;
1364 netdev = alloc_etherdev(sizeof(struct netfront_info));
1365 if (!netdev) {
1366 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1367 __FUNCTION__);
1368 return ERR_PTR(-ENOMEM);
1371 np = netdev_priv(netdev);
1372 np->handle = handle;
1373 np->xbdev = dev;
1375 netif_carrier_off(netdev);
1377 spin_lock_init(&np->tx_lock);
1378 spin_lock_init(&np->rx_lock);
1380 skb_queue_head_init(&np->rx_batch);
1381 np->rx_target = RX_DFL_MIN_TARGET;
1382 np->rx_min_target = RX_DFL_MIN_TARGET;
1383 np->rx_max_target = RX_MAX_TARGET;
1385 init_timer(&np->rx_refill_timer);
1386 np->rx_refill_timer.data = (unsigned long)netdev;
1387 np->rx_refill_timer.function = rx_refill_timeout;
1389 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
1390 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1391 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1392 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1395 for (i = 0; i <= NET_RX_RING_SIZE; i++) {
1396 np->rx_skbs[i] = (void *)((unsigned long) i+1);
1397 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1400 /* A grant for every tx ring slot */
1401 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1402 &np->gref_tx_head) < 0) {
1403 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1404 err = -ENOMEM;
1405 goto exit;
1407 /* A grant for every rx ring slot */
1408 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1409 &np->gref_rx_head) < 0) {
1410 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1411 err = -ENOMEM;
1412 goto exit_free_tx;
1415 netdev->open = network_open;
1416 netdev->hard_start_xmit = network_start_xmit;
1417 netdev->stop = network_close;
1418 netdev->get_stats = network_get_stats;
1419 netdev->poll = netif_poll;
1420 netdev->set_multicast_list = network_set_multicast_list;
1421 netdev->uninit = netif_uninit;
1422 netdev->change_mtu = xennet_change_mtu;
1423 netdev->weight = 64;
1424 netdev->features = NETIF_F_IP_CSUM;
1426 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
1427 SET_MODULE_OWNER(netdev);
1428 SET_NETDEV_DEV(netdev, &dev->dev);
1430 err = register_netdev(netdev);
1431 if (err) {
1432 printk(KERN_WARNING "%s> register_netdev err=%d\n",
1433 __FUNCTION__, err);
1434 goto exit_free_rx;
1437 err = xennet_sysfs_addif(netdev);
1438 if (err) {
1439 /* This can be non-fatal: it only means no tuning parameters */
1440 printk(KERN_WARNING "%s> add sysfs failed err=%d\n",
1441 __FUNCTION__, err);
1444 np->netdev = netdev;
1446 return netdev;
1449 exit_free_rx:
1450 gnttab_free_grant_references(np->gref_rx_head);
1451 exit_free_tx:
1452 gnttab_free_grant_references(np->gref_tx_head);
1453 exit:
1454 free_netdev(netdev);
1455 return ERR_PTR(err);
1458 /*
1459 * We use this notifier to send out a fake ARP reply to reset switches and
1460 * router ARP caches when an IP interface is brought up on a VIF.
1461 */
1462 static int
1463 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1465 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1466 struct net_device *dev = ifa->ifa_dev->dev;
1468 /* UP event and is it one of our devices? */
1469 if (event == NETDEV_UP && dev->open == network_open)
1470 (void)send_fake_arp(dev);
1472 return NOTIFY_DONE;
1476 /* ** Close down ** */
1479 /**
1480 * Handle the change of state of the backend to Closing. We must delete our
1481 * device-layer structures now, to ensure that writes are flushed through to
1482 * the backend. Once is this done, we can switch to Closed in
1483 * acknowledgement.
1484 */
1485 static void netfront_closing(struct xenbus_device *dev)
1487 struct netfront_info *info = dev->dev.driver_data;
1489 DPRINTK("netfront_closing: %s removed\n", dev->nodename);
1491 close_netdev(info);
1493 xenbus_switch_state(dev, XenbusStateClosed);
1497 static int __devexit netfront_remove(struct xenbus_device *dev)
1499 struct netfront_info *info = dev->dev.driver_data;
1501 DPRINTK("%s\n", dev->nodename);
1503 netif_disconnect_backend(info);
1504 free_netdev(info->netdev);
1506 return 0;
1510 static void close_netdev(struct netfront_info *info)
1512 del_timer_sync(&info->rx_refill_timer);
1514 xennet_sysfs_delif(info->netdev);
1515 unregister_netdev(info->netdev);
1519 static void netif_disconnect_backend(struct netfront_info *info)
1521 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1522 spin_lock_irq(&info->tx_lock);
1523 spin_lock(&info->rx_lock);
1524 netif_carrier_off(info->netdev);
1525 spin_unlock(&info->rx_lock);
1526 spin_unlock_irq(&info->tx_lock);
1528 if (info->irq)
1529 unbind_from_irqhandler(info->irq, info->netdev);
1530 info->evtchn = info->irq = 0;
1532 end_access(info->tx_ring_ref, info->tx.sring);
1533 end_access(info->rx_ring_ref, info->rx.sring);
1534 info->tx_ring_ref = GRANT_INVALID_REF;
1535 info->rx_ring_ref = GRANT_INVALID_REF;
1536 info->tx.sring = NULL;
1537 info->rx.sring = NULL;
1541 static void netif_free(struct netfront_info *info)
1543 close_netdev(info);
1544 netif_disconnect_backend(info);
1545 free_netdev(info->netdev);
1549 static void end_access(int ref, void *page)
1551 if (ref != GRANT_INVALID_REF)
1552 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1556 /* ** Driver registration ** */
1559 static struct xenbus_device_id netfront_ids[] = {
1560 { "vif" },
1561 { "" }
1562 };
1565 static struct xenbus_driver netfront = {
1566 .name = "vif",
1567 .owner = THIS_MODULE,
1568 .ids = netfront_ids,
1569 .probe = netfront_probe,
1570 .remove = __devexit_p(netfront_remove),
1571 .resume = netfront_resume,
1572 .otherend_changed = backend_changed,
1573 };
1576 static struct notifier_block notifier_inetdev = {
1577 .notifier_call = inetdev_notify,
1578 .next = NULL,
1579 .priority = 0
1580 };
1582 static int __init netif_init(void)
1584 if (!is_running_on_xen())
1585 return -ENODEV;
1587 if (xen_start_info->flags & SIF_INITDOMAIN)
1588 return 0;
1590 IPRINTK("Initialising virtual ethernet driver.\n");
1592 (void)register_inetaddr_notifier(&notifier_inetdev);
1594 return xenbus_register_frontend(&netfront);
1596 module_init(netif_init);
1599 static void __exit netif_exit(void)
1601 unregister_inetaddr_notifier(&notifier_inetdev);
1603 return xenbus_unregister_driver(&netfront);
1605 module_exit(netif_exit);
1607 MODULE_LICENSE("Dual BSD/GPL");