f72929502171400023305f13b7a23196b0ab258b
[linux-2.6.git] / drivers / xen / netfront / netfront.c
1 /******************************************************************************
2  * Virtual network driver for conversing with remote driver backends.
3  * 
4  * Copyright (c) 2002-2004, K A Fraser
5  * 
6  * This file may be distributed separately from the Linux kernel, or
7  * incorporated into other software packages, subject to the following license:
8  * 
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this source file (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use, copy, modify,
12  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
13  * and to permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  * 
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  * 
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25  * IN THE SOFTWARE.
26  */
27
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/version.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/errno.h>
36 #include <linux/netdevice.h>
37 #include <linux/inetdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/skbuff.h>
40 #include <linux/init.h>
41 #include <linux/bitops.h>
42 #include <net/sock.h>
43 #include <net/pkt_sched.h>
44 #include <net/arp.h>
45 #include <net/route.h>
46 #include <asm/io.h>
47 #include <asm-xen/evtchn.h>
48 #include <asm-xen/ctrl_if.h>
49 #include <asm-xen/xen-public/io/netif.h>
50 #include <asm-xen/balloon.h>
51 #include <asm/page.h>
52
53 #ifndef __GFP_NOWARN
54 #define __GFP_NOWARN 0
55 #endif
56 #define alloc_xen_skb(_l) __dev_alloc_skb((_l), GFP_ATOMIC|__GFP_NOWARN)
57
58 #define init_skb_shinfo(_skb)                         \
59     do {                                              \
60         atomic_set(&(skb_shinfo(_skb)->dataref), 1);  \
61         skb_shinfo(_skb)->nr_frags = 0;               \
62         skb_shinfo(_skb)->frag_list = NULL;           \
63     } while (0)
64
65 /* Allow headroom on each rx pkt for Ethernet header, alignment padding, ... */
66 #define RX_HEADROOM 200
67
68 /*
69  * If the backend driver is pipelining transmit requests then we can be very
70  * aggressive in avoiding new-packet notifications -- only need to send a
71  * notification if there are no outstanding unreceived responses.
72  * If the backend may be buffering our transmit buffers for any reason then we
73  * are rather more conservative.
74  */
75 #ifdef CONFIG_XEN_NETDEV_FRONTEND_PIPELINED_TRANSMITTER
76 #define TX_TEST_IDX resp_prod /* aggressive: any outstanding responses? */
77 #else
78 #define TX_TEST_IDX req_cons  /* conservative: not seen all our requests? */
79 #endif
80
81 static void network_tx_buf_gc(struct net_device *dev);
82 static void network_alloc_rx_buffers(struct net_device *dev);
83
84 static unsigned long rx_pfn_array[NETIF_RX_RING_SIZE];
85 static multicall_entry_t rx_mcl[NETIF_RX_RING_SIZE+1];
86 static mmu_update_t rx_mmu[NETIF_RX_RING_SIZE];
87
88 static struct list_head dev_list;
89
90 struct net_private
91 {
92     struct list_head list;
93     struct net_device *dev;
94
95     struct net_device_stats stats;
96     NETIF_RING_IDX rx_resp_cons, tx_resp_cons;
97     unsigned int tx_full;
98     
99     netif_tx_interface_t *tx;
100     netif_rx_interface_t *rx;
101
102     spinlock_t   tx_lock;
103     spinlock_t   rx_lock;
104
105     unsigned int handle;
106     unsigned int evtchn;
107     unsigned int irq;
108
109     /* What is the status of our connection to the remote backend? */
110 #define BEST_CLOSED       0
111 #define BEST_DISCONNECTED 1
112 #define BEST_CONNECTED    2
113     unsigned int backend_state;
114
115     /* Is this interface open or closed (down or up)? */
116 #define UST_CLOSED        0
117 #define UST_OPEN          1
118     unsigned int user_state;
119
120     /* Receive-ring batched refills. */
121 #define RX_MIN_TARGET 8
122 #define RX_MAX_TARGET NETIF_RX_RING_SIZE
123     int rx_target;
124     struct sk_buff_head rx_batch;
125
126     /*
127      * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
128      * array is an index into a chain of free entries.
129      */
130     struct sk_buff *tx_skbs[NETIF_TX_RING_SIZE+1];
131     struct sk_buff *rx_skbs[NETIF_RX_RING_SIZE+1];
132 };
133
134 /* Access macros for acquiring freeing slots in {tx,rx}_skbs[]. */
135 #define ADD_ID_TO_FREELIST(_list, _id)             \
136     (_list)[(_id)] = (_list)[0];                   \
137     (_list)[0]     = (void *)(unsigned long)(_id);
138 #define GET_ID_FROM_FREELIST(_list)                \
139  ({ unsigned long _id = (unsigned long)(_list)[0]; \
140     (_list)[0]  = (_list)[_id];                    \
141     (unsigned short)_id; })
142
143 static char *status_name[] = {
144     [NETIF_INTERFACE_STATUS_CLOSED]       = "closed",
145     [NETIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected",
146     [NETIF_INTERFACE_STATUS_CONNECTED]    = "connected",
147     [NETIF_INTERFACE_STATUS_CHANGED]      = "changed",
148 };
149
150 static char *be_state_name[] = {
151     [BEST_CLOSED]       = "closed",
152     [BEST_DISCONNECTED] = "disconnected",
153     [BEST_CONNECTED]    = "connected",
154 };
155
156 #if DEBUG
157 #define DPRINTK(fmt, args...) \
158     printk(KERN_ALERT "xen_net (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
159 #else
160 #define DPRINTK(fmt, args...) ((void)0)
161 #endif
162 #define IPRINTK(fmt, args...) \
163     printk(KERN_INFO "xen_net: " fmt, ##args)
164 #define WPRINTK(fmt, args...) \
165     printk(KERN_WARNING "xen_net: " fmt, ##args)
166
167 static struct net_device *find_dev_by_handle(unsigned int handle)
168 {
169     struct list_head *ent;
170     struct net_private *np;
171     list_for_each (ent, &dev_list) {
172         np = list_entry(ent, struct net_private, list);
173         if (np->handle == handle)
174             return np->dev;
175     }
176     return NULL;
177 }
178
179 /** Network interface info. */
180 struct netif_ctrl {
181     /** Number of interfaces. */
182     int interface_n;
183     /** Number of connected interfaces. */
184     int connected_n;
185     /** Error code. */
186     int err;
187     int up;
188 };
189
190 static struct netif_ctrl netctrl;
191
192 static void netctrl_init(void)
193 {
194     memset(&netctrl, 0, sizeof(netctrl));
195     netctrl.up = NETIF_DRIVER_STATUS_DOWN;
196 }
197
198 /** Get or set a network interface error.
199  */
200 static int netctrl_err(int err)
201 {
202     if ((err < 0) && !netctrl.err)
203         netctrl.err = err;
204     return netctrl.err;
205 }
206
207 /** Test if all network interfaces are connected.
208  *
209  * @return 1 if all connected, 0 if not, negative error code otherwise
210  */
211 static int netctrl_connected(void)
212 {
213     int ok;
214
215     if (netctrl.err)
216         ok = netctrl.err;
217     else if (netctrl.up == NETIF_DRIVER_STATUS_UP)
218         ok = (netctrl.connected_n == netctrl.interface_n);
219     else
220         ok = 0;
221
222     return ok;
223 }
224
225 /** Count the connected network interfaces.
226  *
227  * @return connected count
228  */
229 static int netctrl_connected_count(void)
230 {
231     
232     struct list_head *ent;
233     struct net_private *np;
234     unsigned int connected;
235
236     connected = 0;
237     
238     list_for_each(ent, &dev_list) {
239         np = list_entry(ent, struct net_private, list);
240         if (np->backend_state == BEST_CONNECTED)
241             connected++;
242     }
243
244     netctrl.connected_n = connected;
245     DPRINTK("> connected_n=%d interface_n=%d\n",
246             netctrl.connected_n, netctrl.interface_n);
247     return connected;
248 }
249
250 /** Send a packet on a net device to encourage switches to learn the
251  * MAC. We send a fake ARP request.
252  *
253  * @param dev device
254  * @return 0 on success, error code otherwise
255  */
256 static int send_fake_arp(struct net_device *dev)
257 {
258     struct sk_buff *skb;
259     u32             src_ip, dst_ip;
260
261     dst_ip = INADDR_BROADCAST;
262     src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
263
264     /* No IP? Then nothing to do. */
265     if (src_ip == 0)
266         return 0;
267
268     skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
269                      dst_ip, dev, src_ip,
270                      /*dst_hw*/ NULL, /*src_hw*/ NULL, 
271                      /*target_hw*/ dev->dev_addr);
272     if (skb == NULL)
273         return -ENOMEM;
274
275     return dev_queue_xmit(skb);
276 }
277
278 static int network_open(struct net_device *dev)
279 {
280     struct net_private *np = netdev_priv(dev);
281
282     memset(&np->stats, 0, sizeof(np->stats));
283
284     np->user_state = UST_OPEN;
285
286     network_alloc_rx_buffers(dev);
287     np->rx->event = np->rx_resp_cons + 1;
288
289     netif_start_queue(dev);
290
291     return 0;
292 }
293
294 static void network_tx_buf_gc(struct net_device *dev)
295 {
296     NETIF_RING_IDX i, prod;
297     unsigned short id;
298     struct net_private *np = netdev_priv(dev);
299     struct sk_buff *skb;
300
301     if (np->backend_state != BEST_CONNECTED)
302         return;
303
304     do {
305         prod = np->tx->resp_prod;
306         rmb(); /* Ensure we see responses up to 'rp'. */
307
308         for (i = np->tx_resp_cons; i != prod; i++) {
309             id  = np->tx->ring[MASK_NETIF_TX_IDX(i)].resp.id;
310             skb = np->tx_skbs[id];
311             ADD_ID_TO_FREELIST(np->tx_skbs, id);
312             dev_kfree_skb_irq(skb);
313         }
314         
315         np->tx_resp_cons = prod;
316         
317         /*
318          * Set a new event, then check for race with update of tx_cons. Note
319          * that it is essential to schedule a callback, no matter how few
320          * buffers are pending. Even if there is space in the transmit ring,
321          * higher layers may be blocked because too much data is outstanding:
322          * in such cases notification from Xen is likely to be the only kick
323          * that we'll get.
324          */
325         np->tx->event = 
326             prod + ((np->tx->req_prod - prod) >> 1) + 1;
327         mb();
328     } while (prod != np->tx->resp_prod);
329
330     if (np->tx_full && ((np->tx->req_prod - prod) < NETIF_TX_RING_SIZE)) {
331         np->tx_full = 0;
332         if (np->user_state == UST_OPEN)
333             netif_wake_queue(dev);
334     }
335 }
336
337
338 static void network_alloc_rx_buffers(struct net_device *dev)
339 {
340     unsigned short id;
341     struct net_private *np = netdev_priv(dev);
342     struct sk_buff *skb;
343     int i, batch_target;
344     NETIF_RING_IDX req_prod = np->rx->req_prod;
345
346     if (unlikely(np->backend_state != BEST_CONNECTED))
347         return;
348
349     /*
350      * Allocate skbuffs greedily, even though we batch updates to the
351      * receive ring. This creates a less bursty demand on the memory allocator,
352      * so should reduce the chance of failed allocation requests both for
353      * ourself and for other kernel subsystems.
354      */
355     batch_target = np->rx_target - (req_prod - np->rx_resp_cons);
356     for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
357         if (unlikely((skb = alloc_xen_skb(dev->mtu + RX_HEADROOM)) == NULL))
358             break;
359         __skb_queue_tail(&np->rx_batch, skb);
360     }
361
362     /* Is the batch large enough to be worthwhile? */
363     if (i < (np->rx_target/2))
364         return;
365
366     for (i = 0; ; i++) {
367         if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
368             break;
369
370         skb->dev = dev;
371
372         id = GET_ID_FROM_FREELIST(np->rx_skbs);
373
374         np->rx_skbs[id] = skb;
375         
376         np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.id = id;
377         
378         rx_pfn_array[i] = virt_to_machine(skb->head) >> PAGE_SHIFT;
379
380         /* Remove this page from pseudo phys map before passing back to Xen. */
381         phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] 
382             = INVALID_P2M_ENTRY;
383
384         rx_mcl[i].op = __HYPERVISOR_update_va_mapping;
385         rx_mcl[i].args[0] = (unsigned long)skb->head;
386         rx_mcl[i].args[1] = 0;
387         rx_mcl[i].args[2] = 0;
388     }
389
390     /* After all PTEs have been zapped we blow away stale TLB entries. */
391     rx_mcl[i-1].args[2] = UVMF_TLB_FLUSH|UVMF_ALL;
392
393     /* Give away a batch of pages. */
394     rx_mcl[i].op = __HYPERVISOR_dom_mem_op;
395     rx_mcl[i].args[0] = MEMOP_decrease_reservation;
396     rx_mcl[i].args[1] = (unsigned long)rx_pfn_array;
397     rx_mcl[i].args[2] = (unsigned long)i;
398     rx_mcl[i].args[3] = 0;
399     rx_mcl[i].args[4] = DOMID_SELF;
400
401     /* Tell the ballon driver what is going on. */
402     balloon_update_driver_allowance(i);
403
404     /* Zap PTEs and give away pages in one big multicall. */
405     (void)HYPERVISOR_multicall(rx_mcl, i+1);
406
407     /* Check return status of HYPERVISOR_dom_mem_op(). */
408     if (unlikely(rx_mcl[i].args[5] != i))
409         panic("Unable to reduce memory reservation\n");
410
411     /* Above is a suitable barrier to ensure backend will see requests. */
412     np->rx->req_prod = req_prod + i;
413
414     /* Adjust our floating fill target if we risked running out of buffers. */
415     if (((req_prod - np->rx->resp_prod) < (np->rx_target / 4)) &&
416          ((np->rx_target *= 2) > RX_MAX_TARGET))
417         np->rx_target = RX_MAX_TARGET;
418 }
419
420
421 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
422 {
423     unsigned short id;
424     struct net_private *np = netdev_priv(dev);
425     netif_tx_request_t *tx;
426     NETIF_RING_IDX i;
427
428     if (unlikely(np->tx_full)) {
429         printk(KERN_ALERT "%s: full queue wasn't stopped!\n", dev->name);
430         netif_stop_queue(dev);
431         goto drop;
432     }
433
434     if (unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
435                   PAGE_SIZE)) {
436         struct sk_buff *nskb;
437         if (unlikely((nskb = alloc_xen_skb(skb->len)) == NULL))
438             goto drop;
439         skb_put(nskb, skb->len);
440         memcpy(nskb->data, skb->data, skb->len);
441         nskb->dev = skb->dev;
442         dev_kfree_skb(skb);
443         skb = nskb;
444     }
445     
446     spin_lock_irq(&np->tx_lock);
447
448     if (np->backend_state != BEST_CONNECTED) {
449         spin_unlock_irq(&np->tx_lock);
450         goto drop;
451     }
452
453     i = np->tx->req_prod;
454
455     id = GET_ID_FROM_FREELIST(np->tx_skbs);
456     np->tx_skbs[id] = skb;
457
458     tx = &np->tx->ring[MASK_NETIF_TX_IDX(i)].req;
459
460     tx->id   = id;
461     tx->addr = virt_to_machine(skb->data);
462     tx->size = skb->len;
463
464     wmb(); /* Ensure that backend will see the request. */
465     np->tx->req_prod = i + 1;
466
467     network_tx_buf_gc(dev);
468
469     if ((i - np->tx_resp_cons) == (NETIF_TX_RING_SIZE - 1)) {
470         np->tx_full = 1;
471         netif_stop_queue(dev);
472     }
473
474     spin_unlock_irq(&np->tx_lock);
475
476     np->stats.tx_bytes += skb->len;
477     np->stats.tx_packets++;
478
479     /* Only notify Xen if we really have to. */
480     mb();
481     if (np->tx->TX_TEST_IDX == i)
482         notify_via_evtchn(np->evtchn);
483
484     return 0;
485
486  drop:
487     np->stats.tx_dropped++;
488     dev_kfree_skb(skb);
489     return 0;
490 }
491
492 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
493 {
494     struct net_device *dev = dev_id;
495     struct net_private *np = netdev_priv(dev);
496     unsigned long flags;
497
498     spin_lock_irqsave(&np->tx_lock, flags);
499     network_tx_buf_gc(dev);
500     spin_unlock_irqrestore(&np->tx_lock, flags);
501
502     if ((np->rx_resp_cons != np->rx->resp_prod) && (np->user_state == UST_OPEN))
503         netif_rx_schedule(dev);
504
505     return IRQ_HANDLED;
506 }
507
508
509 static int netif_poll(struct net_device *dev, int *pbudget)
510 {
511     struct net_private *np = netdev_priv(dev);
512     struct sk_buff *skb, *nskb;
513     netif_rx_response_t *rx;
514     NETIF_RING_IDX i, rp;
515     mmu_update_t *mmu = rx_mmu;
516     multicall_entry_t *mcl = rx_mcl;
517     int work_done, budget, more_to_do = 1;
518     struct sk_buff_head rxq;
519     unsigned long flags;
520
521     spin_lock(&np->rx_lock);
522
523     if (np->backend_state != BEST_CONNECTED) {
524         spin_unlock(&np->rx_lock);
525         return 0;
526     }
527
528     skb_queue_head_init(&rxq);
529
530     if ((budget = *pbudget) > dev->quota)
531         budget = dev->quota;
532
533     rp = np->rx->resp_prod;
534     rmb(); /* Ensure we see queued responses up to 'rp'. */
535
536     for (i = np->rx_resp_cons, work_done = 0; 
537                     (i != rp) && (work_done < budget);
538                     i++, work_done++) {
539         rx = &np->rx->ring[MASK_NETIF_RX_IDX(i)].resp;
540
541         /*
542          * An error here is very odd. Usually indicates a backend bug,
543          * low-memory condition, or that we didn't have reservation headroom.
544          */
545         if (unlikely(rx->status <= 0)) {
546             if (net_ratelimit())
547                 printk(KERN_WARNING "Bad rx buffer (memory squeeze?).\n");
548             np->rx->ring[MASK_NETIF_RX_IDX(np->rx->req_prod)].req.id = rx->id;
549             wmb();
550             np->rx->req_prod++;
551             work_done--;
552             continue;
553         }
554
555         skb = np->rx_skbs[rx->id];
556         ADD_ID_TO_FREELIST(np->rx_skbs, rx->id);
557
558         /* NB. We handle skb overflow later. */
559         skb->data = skb->head + (rx->addr & ~PAGE_MASK);
560         skb->len  = rx->status;
561         skb->tail = skb->data + skb->len;
562
563         np->stats.rx_packets++;
564         np->stats.rx_bytes += rx->status;
565
566         /* Remap the page. */
567         mmu->ptr  = (rx->addr & PAGE_MASK) | MMU_MACHPHYS_UPDATE;
568         mmu->val  = __pa(skb->head) >> PAGE_SHIFT;
569         mmu++;
570         mcl->op = __HYPERVISOR_update_va_mapping;
571         mcl->args[0] = (unsigned long)skb->head;
572         mcl->args[1] = (rx->addr & PAGE_MASK) | __PAGE_KERNEL;
573         mcl->args[2] = 0;
574         mcl++;
575
576         phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] = 
577             rx->addr >> PAGE_SHIFT;
578
579         __skb_queue_tail(&rxq, skb);
580     }
581
582     /* Some pages are no longer absent... */
583     balloon_update_driver_allowance(-work_done);
584
585     /* Do all the remapping work, and M->P updates, in one big hypercall. */
586     if (likely((mcl - rx_mcl) != 0)) {
587         mcl->op = __HYPERVISOR_mmu_update;
588         mcl->args[0] = (unsigned long)rx_mmu;
589         mcl->args[1] = mmu - rx_mmu;
590         mcl->args[2] = 0;
591         mcl->args[3] = DOMID_SELF;
592         mcl++;
593         (void)HYPERVISOR_multicall(rx_mcl, mcl - rx_mcl);
594     }
595
596     while ((skb = __skb_dequeue(&rxq)) != NULL) {
597         /*
598          * Enough room in skbuff for the data we were passed? Also, Linux 
599          * expects at least 16 bytes headroom in each receive buffer.
600          */
601         if (unlikely(skb->tail > skb->end) || 
602                         unlikely((skb->data - skb->head) < 16)) {
603             nskb = NULL;
604
605             /* Only copy the packet if it fits in the current MTU. */
606             if (skb->len <= (dev->mtu + ETH_HLEN)) {
607                 if ((skb->tail > skb->end) && net_ratelimit())
608                     printk(KERN_INFO "Received packet needs %d bytes more "
609                            "headroom.\n", skb->tail - skb->end);
610
611                 if ((nskb = alloc_xen_skb(skb->len + 2)) != NULL) {
612                     skb_reserve(nskb, 2);
613                     skb_put(nskb, skb->len);
614                     memcpy(nskb->data, skb->data, skb->len);
615                     nskb->dev = skb->dev;
616                 }
617             }
618             else if (net_ratelimit())
619                 printk(KERN_INFO "Received packet too big for MTU "
620                        "(%d > %d)\n", skb->len - ETH_HLEN, dev->mtu);
621
622             /* Reinitialise and then destroy the old skbuff. */
623             skb->len  = 0;
624             skb->tail = skb->data;
625             init_skb_shinfo(skb);
626             dev_kfree_skb(skb);
627
628             /* Switch old for new, if we copied the buffer. */
629             if ((skb = nskb) == NULL)
630                 continue;
631         }
632         
633         /* Set the shared-info area, which is hidden behind the real data. */
634         init_skb_shinfo(skb);
635
636         /* Ethernet-specific work. Delayed to here as it peeks the header. */
637         skb->protocol = eth_type_trans(skb, dev);
638
639         /* Pass it up. */
640         netif_receive_skb(skb);
641         dev->last_rx = jiffies;
642     }
643
644     np->rx_resp_cons = i;
645
646     /* If we get a callback with very few responses, reduce fill target. */
647     /* NB. Note exponential increase, linear decrease. */
648     if (((np->rx->req_prod - np->rx->resp_prod) > ((3*np->rx_target) / 4)) &&
649          (--np->rx_target < RX_MIN_TARGET))
650         np->rx_target = RX_MIN_TARGET;
651
652     network_alloc_rx_buffers(dev);
653
654     *pbudget   -= work_done;
655     dev->quota -= work_done;
656
657     if (work_done < budget) {
658         local_irq_save(flags);
659
660         np->rx->event = i + 1;
661     
662         /* Deal with hypervisor racing our resetting of rx_event. */
663         mb();
664         if (np->rx->resp_prod == i) {
665             __netif_rx_complete(dev);
666             more_to_do = 0;
667         }
668
669         local_irq_restore(flags);
670     }
671
672     spin_unlock(&np->rx_lock);
673
674     return more_to_do;
675 }
676
677
678 static int network_close(struct net_device *dev)
679 {
680     struct net_private *np = netdev_priv(dev);
681     np->user_state = UST_CLOSED;
682     netif_stop_queue(np->dev);
683     return 0;
684 }
685
686
687 static struct net_device_stats *network_get_stats(struct net_device *dev)
688 {
689     struct net_private *np = netdev_priv(dev);
690     return &np->stats;
691 }
692
693
694 static void network_connect(struct net_device *dev,
695                             netif_fe_interface_status_t *status)
696 {
697     struct net_private *np;
698     int i, requeue_idx;
699     netif_tx_request_t *tx;
700
701     np = netdev_priv(dev);
702     spin_lock_irq(&np->tx_lock);
703     spin_lock(&np->rx_lock);
704
705     /* Recovery procedure: */
706
707     /* Step 1: Reinitialise variables. */
708     np->rx_resp_cons = np->tx_resp_cons = np->tx_full = 0;
709     np->rx->event = np->tx->event = 1;
710
711     /* Step 2: Rebuild the RX and TX ring contents.
712      * NB. We could just free the queued TX packets now but we hope
713      * that sending them out might do some good.  We have to rebuild
714      * the RX ring because some of our pages are currently flipped out
715      * so we can't just free the RX skbs.
716      * NB2. Freelist index entries are always going to be less than
717      *  __PAGE_OFFSET, whereas pointers to skbs will always be equal or
718      * greater than __PAGE_OFFSET: we use this property to distinguish
719      * them.
720      */
721
722     /* Rebuild the TX buffer freelist and the TX ring itself.
723      * NB. This reorders packets.  We could keep more private state
724      * to avoid this but maybe it doesn't matter so much given the
725      * interface has been down.
726      */
727     for (requeue_idx = 0, i = 1; i <= NETIF_TX_RING_SIZE; i++) {
728             if ((unsigned long)np->tx_skbs[i] >= __PAGE_OFFSET) {
729                 struct sk_buff *skb = np->tx_skbs[i];
730                 
731                 tx = &np->tx->ring[requeue_idx++].req;
732                 
733                 tx->id   = i;
734                 tx->addr = virt_to_machine(skb->data);
735                 tx->size = skb->len;
736                 
737                 np->stats.tx_bytes += skb->len;
738                 np->stats.tx_packets++;
739             }
740     }
741     wmb();
742     np->tx->req_prod = requeue_idx;
743
744     /* Rebuild the RX buffer freelist and the RX ring itself. */
745     for (requeue_idx = 0, i = 1; i <= NETIF_RX_RING_SIZE; i++)
746         if ((unsigned long)np->rx_skbs[i] >= __PAGE_OFFSET)
747             np->rx->ring[requeue_idx++].req.id = i;
748     wmb();                
749     np->rx->req_prod = requeue_idx;
750
751     /* Step 3: All public and private state should now be sane.  Get
752      * ready to start sending and receiving packets and give the driver
753      * domain a kick because we've probably just requeued some
754      * packets.
755      */
756     np->backend_state = BEST_CONNECTED;
757     wmb();
758     notify_via_evtchn(status->evtchn);  
759     network_tx_buf_gc(dev);
760
761     if (np->user_state == UST_OPEN)
762         netif_start_queue(dev);
763
764     spin_unlock(&np->rx_lock);
765     spin_unlock_irq(&np->tx_lock);
766 }
767
768 static void vif_show(struct net_private *np)
769 {
770 #if DEBUG
771     if (np) {
772         IPRINTK("<vif handle=%u %s(%s) evtchn=%u irq=%u tx=%p rx=%p>\n",
773                np->handle,
774                be_state_name[np->backend_state],
775                np->user_state ? "open" : "closed",
776                np->evtchn,
777                np->irq,
778                np->tx,
779                np->rx);
780     } else {
781         IPRINTK("<vif NULL>\n");
782     }
783 #endif
784 }
785
786 /* Send a connect message to xend to tell it to bring up the interface. */
787 static void send_interface_connect(struct net_private *np)
788 {
789     ctrl_msg_t cmsg = {
790         .type    = CMSG_NETIF_FE,
791         .subtype = CMSG_NETIF_FE_INTERFACE_CONNECT,
792         .length  = sizeof(netif_fe_interface_connect_t),
793     };
794     netif_fe_interface_connect_t *msg = (void*)cmsg.msg;
795
796     msg->handle = np->handle;
797     msg->tx_shmem_frame = (virt_to_machine(np->tx) >> PAGE_SHIFT);
798     msg->rx_shmem_frame = (virt_to_machine(np->rx) >> PAGE_SHIFT);
799         
800     ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
801 }
802
803 /* Send a driver status notification to the domain controller. */
804 static int send_driver_status(int ok)
805 {
806     int err = 0;
807     ctrl_msg_t cmsg = {
808         .type    = CMSG_NETIF_FE,
809         .subtype = CMSG_NETIF_FE_DRIVER_STATUS,
810         .length  = sizeof(netif_fe_driver_status_t),
811     };
812     netif_fe_driver_status_t *msg = (void*)cmsg.msg;
813
814     msg->status = (ok ? NETIF_DRIVER_STATUS_UP : NETIF_DRIVER_STATUS_DOWN);
815     err = ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
816     return err;
817 }
818
819 /* Stop network device and free tx/rx queues and irq.
820  */
821 static void vif_release(struct net_private *np)
822 {
823     /* Stop old i/f to prevent errors whilst we rebuild the state. */
824     spin_lock_irq(&np->tx_lock);
825     spin_lock(&np->rx_lock);
826     netif_stop_queue(np->dev);
827     /* np->backend_state = BEST_DISCONNECTED; */
828     spin_unlock(&np->rx_lock);
829     spin_unlock_irq(&np->tx_lock);
830     
831     /* Free resources. */
832     if(np->tx != NULL){
833         free_irq(np->irq, np->dev);
834         unbind_evtchn_from_irq(np->evtchn);
835         free_page((unsigned long)np->tx);
836         free_page((unsigned long)np->rx);
837         np->irq = 0;
838         np->evtchn = 0;
839         np->tx = NULL;
840         np->rx = NULL;
841     }
842 }
843
844 /* Release vif resources and close it down completely.
845  */
846 static void vif_close(struct net_private *np)
847 {
848     WPRINTK("Unexpected netif-CLOSED message in state %s\n",
849             be_state_name[np->backend_state]);
850     vif_release(np);
851     np->backend_state = BEST_CLOSED;
852     /* todo: take dev down and free. */
853     vif_show(np);
854 }
855
856 /* Move the vif into disconnected state.
857  * Allocates tx/rx pages.
858  * Sends connect message to xend.
859  */
860 static void vif_disconnect(struct net_private *np)
861 {
862     if(np->tx) free_page((unsigned long)np->tx);
863     if(np->rx) free_page((unsigned long)np->rx);
864     // Before this np->tx and np->rx had better be null.
865     np->tx = (netif_tx_interface_t *)__get_free_page(GFP_KERNEL);
866     np->rx = (netif_rx_interface_t *)__get_free_page(GFP_KERNEL);
867     memset(np->tx, 0, PAGE_SIZE);
868     memset(np->rx, 0, PAGE_SIZE);
869     np->backend_state = BEST_DISCONNECTED;
870     send_interface_connect(np);
871     vif_show(np);
872 }
873
874 /* Begin interface recovery.
875  *
876  * NB. Whilst we're recovering, we turn the carrier state off.  We
877  * take measures to ensure that this device isn't used for
878  * anything.  We also stop the queue for this device.  Various
879  * different approaches (e.g. continuing to buffer packets) have
880  * been tested but don't appear to improve the overall impact on
881  * TCP connections.
882  *
883  * TODO: (MAW) Change the Xend<->Guest protocol so that a recovery
884  * is initiated by a special "RESET" message - disconnect could
885  * just mean we're not allowed to use this interface any more.
886  */
887 static void vif_reset(struct net_private *np)
888 {
889     IPRINTK("Attempting to reconnect network interface: handle=%u\n",
890             np->handle);    
891     vif_release(np);
892     vif_disconnect(np);
893     vif_show(np);
894 }
895
896 /* Move the vif into connected state.
897  * Sets the mac and event channel from the message.
898  * Binds the irq to the event channel.
899  */
900 static void 
901 vif_connect(struct net_private *np, netif_fe_interface_status_t *status)
902 {
903     struct net_device *dev = np->dev;
904     memcpy(dev->dev_addr, status->mac, ETH_ALEN);
905     network_connect(dev, status);
906     np->evtchn = status->evtchn;
907     np->irq = bind_evtchn_to_irq(np->evtchn);
908     (void)request_irq(np->irq, netif_int, SA_SAMPLE_RANDOM, dev->name, dev);
909     netctrl_connected_count();
910     (void)send_fake_arp(dev);
911     vif_show(np);
912 }
913
914
915 /** Create a network device.
916  * @param handle device handle
917  * @param val return parameter for created device
918  * @return 0 on success, error code otherwise
919  */
920 static int create_netdev(int handle, struct net_device **val)
921 {
922     int i, err = 0;
923     struct net_device *dev = NULL;
924     struct net_private *np = NULL;
925
926     if ((dev = alloc_etherdev(sizeof(struct net_private))) == NULL) {
927         printk(KERN_WARNING "%s> alloc_etherdev failed.\n", __FUNCTION__);
928         err = -ENOMEM;
929         goto exit;
930     }
931
932     np                = netdev_priv(dev);
933     np->backend_state = BEST_CLOSED;
934     np->user_state    = UST_CLOSED;
935     np->handle        = handle;
936     
937     spin_lock_init(&np->tx_lock);
938     spin_lock_init(&np->rx_lock);
939
940     skb_queue_head_init(&np->rx_batch);
941     np->rx_target = RX_MIN_TARGET;
942
943     /* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
944     for (i = 0; i <= NETIF_TX_RING_SIZE; i++)
945         np->tx_skbs[i] = (void *)(i+1);
946     for (i = 0; i <= NETIF_RX_RING_SIZE; i++)
947         np->rx_skbs[i] = (void *)(i+1);
948
949     dev->open            = network_open;
950     dev->hard_start_xmit = network_start_xmit;
951     dev->stop            = network_close;
952     dev->get_stats       = network_get_stats;
953     dev->poll            = netif_poll;
954     dev->weight          = 64;
955     
956     if ((err = register_netdev(dev)) != 0) {
957         printk(KERN_WARNING "%s> register_netdev err=%d\n", __FUNCTION__, err);
958         goto exit;
959     }
960     np->dev = dev;
961     list_add(&np->list, &dev_list);
962
963   exit:
964     if ((err != 0) && (dev != NULL ))
965         kfree(dev);
966     else if (val != NULL)
967         *val = dev;
968     return err;
969 }
970
971 /* Get the target interface for a status message.
972  * Creates the interface when it makes sense.
973  * The returned interface may be null when there is no error.
974  *
975  * @param status status message
976  * @param np return parameter for interface state
977  * @return 0 on success, error code otherwise
978  */
979 static int 
980 target_vif(netif_fe_interface_status_t *status, struct net_private **np)
981 {
982     int err = 0;
983     struct net_device *dev;
984
985     DPRINTK("> handle=%d\n", status->handle);
986     if (status->handle < 0) {
987         err = -EINVAL;
988         goto exit;
989     }
990
991     if ((dev = find_dev_by_handle(status->handle)) != NULL)
992         goto exit;
993
994     if (status->status == NETIF_INTERFACE_STATUS_CLOSED)
995         goto exit;
996     if (status->status == NETIF_INTERFACE_STATUS_CHANGED)
997         goto exit;
998
999     /* It's a new interface in a good state - create it. */
1000     DPRINTK("> create device...\n");
1001     if ((err = create_netdev(status->handle, &dev)) != 0)
1002         goto exit;
1003
1004     netctrl.interface_n++;
1005
1006   exit:
1007     if (np != NULL)
1008         *np = ((dev && !err) ? netdev_priv(dev) : NULL);
1009     DPRINTK("< err=%d\n", err);
1010     return err;
1011 }
1012
1013 /* Handle an interface status message. */
1014 static void netif_interface_status(netif_fe_interface_status_t *status)
1015 {
1016     int err = 0;
1017     struct net_private *np = NULL;
1018     
1019     DPRINTK("> status=%s handle=%d\n",
1020             status_name[status->status], status->handle);
1021
1022     if ((err = target_vif(status, &np)) != 0) {
1023         WPRINTK("Invalid netif: handle=%u\n", status->handle);
1024         return;
1025     }
1026
1027     if (np == NULL) {
1028         DPRINTK("> no vif\n");
1029         return;
1030     }
1031
1032     switch (status->status) {
1033     case NETIF_INTERFACE_STATUS_CLOSED:
1034         switch (np->backend_state) {
1035         case BEST_CLOSED:
1036         case BEST_DISCONNECTED:
1037         case BEST_CONNECTED:
1038             vif_close(np);
1039             break;
1040         }
1041         break;
1042
1043     case NETIF_INTERFACE_STATUS_DISCONNECTED:
1044         switch (np->backend_state) {
1045         case BEST_CLOSED:
1046             vif_disconnect(np);
1047             break;
1048         case BEST_DISCONNECTED:
1049         case BEST_CONNECTED:
1050             vif_reset(np);
1051             break;
1052         }
1053         break;
1054
1055     case NETIF_INTERFACE_STATUS_CONNECTED:
1056         switch (np->backend_state) {
1057         case BEST_CLOSED:
1058             WPRINTK("Unexpected netif status %s in state %s\n",
1059                     status_name[status->status],
1060                     be_state_name[np->backend_state]);
1061             vif_disconnect(np);
1062             vif_connect(np, status);
1063             break;
1064         case BEST_DISCONNECTED:
1065             vif_connect(np, status);
1066             break;
1067         }
1068         break;
1069
1070     case NETIF_INTERFACE_STATUS_CHANGED:
1071         /*
1072          * The domain controller is notifying us that a device has been
1073          * added or removed.
1074          */
1075         break;
1076
1077     default:
1078         WPRINTK("Invalid netif status code %d\n", status->status);
1079         break;
1080     }
1081
1082     vif_show(np);
1083 }
1084
1085 /*
1086  * Initialize the network control interface. 
1087  */
1088 static void netif_driver_status(netif_fe_driver_status_t *status)
1089 {
1090     netctrl.up = status->status;
1091     netctrl_connected_count();
1092 }
1093
1094 /* Receive handler for control messages. */
1095 static void netif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id)
1096 {
1097
1098     switch (msg->subtype) {
1099     case CMSG_NETIF_FE_INTERFACE_STATUS:
1100         netif_interface_status((netif_fe_interface_status_t *) &msg->msg[0]);
1101         break;
1102
1103     case CMSG_NETIF_FE_DRIVER_STATUS:
1104         netif_driver_status((netif_fe_driver_status_t *) &msg->msg[0]);
1105         break;
1106
1107     default:
1108         msg->length = 0;
1109         break;
1110     }
1111
1112     ctrl_if_send_response(msg);
1113 }
1114
1115
1116 #if 1
1117 /* Wait for all interfaces to be connected.
1118  *
1119  * This works OK, but we'd like to use the probing mode (see below).
1120  */
1121 static int probe_interfaces(void)
1122 {
1123     int err = 0, conn = 0;
1124     int wait_i, wait_n = 100;
1125
1126     DPRINTK(">\n");
1127
1128     for (wait_i = 0; wait_i < wait_n; wait_i++) { 
1129         DPRINTK("> wait_i=%d\n", wait_i);
1130         conn = netctrl_connected();
1131         if(conn) break;
1132         DPRINTK("> schedule_timeout...\n");
1133         set_current_state(TASK_INTERRUPTIBLE);
1134         schedule_timeout(10);
1135     }
1136
1137     DPRINTK("> wait finished...\n");
1138     if (conn <= 0) {
1139         err = netctrl_err(-ENETDOWN);
1140         WPRINTK("Failed to connect all virtual interfaces: err=%d\n", err);
1141     }
1142
1143     DPRINTK("< err=%d\n", err);
1144
1145     return err;
1146 }
1147 #else
1148 /* Probe for interfaces until no more are found.
1149  *
1150  * This is the mode we'd like to use, but at the moment it panics the kernel.
1151 */
1152 static int probe_interfaces(void)
1153 {
1154     int err = 0;
1155     int wait_i, wait_n = 100;
1156     ctrl_msg_t cmsg = {
1157         .type    = CMSG_NETIF_FE,
1158         .subtype = CMSG_NETIF_FE_INTERFACE_STATUS,
1159         .length  = sizeof(netif_fe_interface_status_t),
1160     };
1161     netif_fe_interface_status_t msg = {};
1162     ctrl_msg_t rmsg = {};
1163     netif_fe_interface_status_t *reply = (void*)rmsg.msg;
1164     int state = TASK_UNINTERRUPTIBLE;
1165     u32 query = -1;
1166
1167     DPRINTK(">\n");
1168
1169     netctrl.interface_n = 0;
1170     for (wait_i = 0; wait_i < wait_n; wait_i++) { 
1171         DPRINTK("> wait_i=%d query=%d\n", wait_i, query);
1172         msg.handle = query;
1173         memcpy(cmsg.msg, &msg, sizeof(msg));
1174         DPRINTK("> set_current_state...\n");
1175         set_current_state(state);
1176         DPRINTK("> rmsg=%p msg=%p, reply=%p\n", &rmsg, rmsg.msg, reply);
1177         DPRINTK("> sending...\n");
1178         err = ctrl_if_send_message_and_get_response(&cmsg, &rmsg, state);
1179         DPRINTK("> err=%d\n", err);
1180         if(err) goto exit;
1181         DPRINTK("> rmsg=%p msg=%p, reply=%p\n", &rmsg, rmsg.msg, reply);
1182         if((int)reply->handle < 0) {
1183             // No more interfaces.
1184             break;
1185         }
1186         query = -reply->handle - 2;
1187         DPRINTK(">netif_interface_status ...\n");
1188         netif_interface_status(reply);
1189     }
1190
1191   exit:
1192     if (err) {
1193         err = netctrl_err(-ENETDOWN);
1194         WPRINTK("Connecting virtual network interfaces failed: err=%d\n", err);
1195     }
1196
1197     DPRINTK("< err=%d\n", err);
1198     return err;
1199 }
1200
1201 #endif
1202
1203 /*
1204  * We use this notifier to send out a fake ARP reply to reset switches and
1205  * router ARP caches when an IP interface is brought up on a VIF.
1206  */
1207 static int 
1208 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1209 {
1210     struct in_ifaddr  *ifa = (struct in_ifaddr *)ptr; 
1211     struct net_device *dev = ifa->ifa_dev->dev;
1212     struct list_head  *ent;
1213     struct net_private *np;
1214
1215     if (event != NETDEV_UP)
1216         goto out;
1217
1218     list_for_each (ent, &dev_list) {
1219         np = list_entry(ent, struct net_private, list);
1220         if (np->dev == dev)
1221             (void)send_fake_arp(dev);
1222     }
1223         
1224  out:
1225     return NOTIFY_DONE;
1226 }
1227
1228 static struct notifier_block notifier_inetdev = {
1229     .notifier_call  = inetdev_notify,
1230     .next           = NULL,
1231     .priority       = 0
1232 };
1233
1234 static int __init netif_init(void)
1235 {
1236     int err = 0;
1237
1238     if (xen_start_info.flags & SIF_INITDOMAIN)
1239         return 0;
1240
1241     IPRINTK("Initialising virtual ethernet driver.\n");
1242     INIT_LIST_HEAD(&dev_list);
1243     (void)register_inetaddr_notifier(&notifier_inetdev);
1244     netctrl_init();
1245     (void)ctrl_if_register_receiver(CMSG_NETIF_FE, netif_ctrlif_rx,
1246                                     CALLBACK_IN_BLOCKING_CONTEXT);
1247     send_driver_status(1);
1248     err = probe_interfaces();
1249     if (err)
1250         ctrl_if_unregister_receiver(CMSG_NETIF_FE, netif_ctrlif_rx);
1251
1252     DPRINTK("< err=%d\n", err);
1253     return err;
1254 }
1255
1256 static void vif_suspend(struct net_private *np)
1257 {
1258     /* Avoid having tx/rx stuff happen until we're ready. */
1259     free_irq(np->irq, np->dev);
1260     unbind_evtchn_from_irq(np->evtchn);
1261 }
1262
1263 static void vif_resume(struct net_private *np)
1264 {
1265     /*
1266      * Connect regardless of whether IFF_UP flag set.
1267      * Stop bad things from happening until we're back up.
1268      */
1269     np->backend_state = BEST_DISCONNECTED;
1270     memset(np->tx, 0, PAGE_SIZE);
1271     memset(np->rx, 0, PAGE_SIZE);
1272     
1273     send_interface_connect(np);
1274 }
1275
1276 void netif_suspend(void)
1277 {
1278     struct list_head *ent;
1279     struct net_private *np;
1280     
1281     list_for_each (ent, &dev_list) {
1282         np = list_entry(ent, struct net_private, list);
1283         vif_suspend(np);
1284     }
1285 }
1286
1287 void netif_resume(void)
1288 {
1289     struct list_head *ent;
1290     struct net_private *np;
1291
1292     list_for_each (ent, &dev_list) {
1293         np = list_entry(ent, struct net_private, list);
1294         vif_resume(np);
1295     }
1296 }
1297
1298
1299 module_init(netif_init);
1300