]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/xen/netfront/netfront.c
register netfront before xenbus does its probing
[FreeBSD/FreeBSD.git] / sys / dev / xen / netfront / netfront.c
1 /*
2  *
3  * Copyright (c) 2004-2006 Kip Macy
4  * All rights reserved.
5  *
6  *
7  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
8  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
9  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
10  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
11  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
12  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
16  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  */
18
19
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/sockio.h>
26 #include <sys/mbuf.h>
27 #include <sys/malloc.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/queue.h>
31 #include <sys/sx.h>
32
33 #include <net/if.h>
34 #include <net/if_arp.h>
35 #include <net/ethernet.h>
36 #include <net/if_dl.h>
37 #include <net/if_media.h>
38
39 #include <net/bpf.h>
40
41 #include <net/if_types.h>
42 #include <net/if.h>
43
44 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <netinet/if_ether.h>
48
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51
52 #include <machine/clock.h>      /* for DELAY */
53 #include <machine/bus.h>
54 #include <machine/resource.h>
55 #include <machine/frame.h>
56
57
58 #include <sys/bus.h>
59 #include <sys/rman.h>
60
61 #include <machine/intr_machdep.h>
62
63 #include <machine/xen/xen-os.h>
64 #include <machine/xen/hypervisor.h>
65 #include <machine/xen/xen_intr.h>
66 #include <machine/xen/evtchn.h>
67 #include <machine/xen/xenbus.h>
68 #include <xen/gnttab.h>
69 #include <xen/interface/memory.h>
70 #include <dev/xen/netfront/mbufq.h>
71 #include <machine/xen/features.h>
72 #include <xen/interface/io/netif.h>
73
74
75 #define GRANT_INVALID_REF       0
76
77 #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
78 #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
79
80 #ifdef CONFIG_XEN
81 static int MODPARM_rx_copy = 0;
82 module_param_named(rx_copy, MODPARM_rx_copy, bool, 0);
83 MODULE_PARM_DESC(rx_copy, "Copy packets from network card (rather than flip)");
84 static int MODPARM_rx_flip = 0;
85 module_param_named(rx_flip, MODPARM_rx_flip, bool, 0);
86 MODULE_PARM_DESC(rx_flip, "Flip packets from network card (rather than copy)");
87 #else
88 static const int MODPARM_rx_copy = 1;
89 static const int MODPARM_rx_flip = 0;
90 #endif
91
92 #define RX_COPY_THRESHOLD 256
93
94 #define net_ratelimit() 0
95
96 struct netfront_info;
97 struct netfront_rx_info;
98
99 static void xn_txeof(struct netfront_info *);
100 static void xn_rxeof(struct netfront_info *);
101 static void network_alloc_rx_buffers(struct netfront_info *);
102
103 static void xn_tick_locked(struct netfront_info *);
104 static void xn_tick(void *);
105
106 static void xn_intr(void *);
107 static void xn_start_locked(struct ifnet *);
108 static void xn_start(struct ifnet *);
109 static int  xn_ioctl(struct ifnet *, u_long, caddr_t);
110 static void xn_ifinit_locked(struct netfront_info *);
111 static void xn_ifinit(void *);
112 static void xn_stop(struct netfront_info *);
113 #ifdef notyet
114 static void xn_watchdog(struct ifnet *);
115 #endif
116
117 static void show_device(struct netfront_info *sc);
118 #ifdef notyet
119 static void netfront_closing(struct xenbus_device *dev);
120 #endif
121 static void netif_free(struct netfront_info *info);
122 static int netfront_remove(struct xenbus_device *dev);
123
124 static int talk_to_backend(struct xenbus_device *dev, struct netfront_info *info);
125 static int create_netdev(struct xenbus_device *dev, struct ifnet **ifp);
126 static void netif_disconnect_backend(struct netfront_info *info);
127 static int setup_device(struct xenbus_device *dev, struct netfront_info *info);
128 static void end_access(int ref, void *page);
129
130 /* Xenolinux helper functions */
131 static int network_connect(struct ifnet *ifp);
132
133 static void xn_free_rx_ring(struct netfront_info *);
134
135 static void xn_free_tx_ring(struct netfront_info *);
136
137 static int xennet_get_responses(struct netfront_info *np,
138         struct netfront_rx_info *rinfo, RING_IDX rp, struct mbuf_head *list,
139         int *pages_flipped_p);
140
141 #define virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT)
142
143 #define INVALID_P2M_ENTRY (~0UL)
144
145 /*
146  * Mbuf pointers. We need these to keep track of the virtual addresses
147  * of our mbuf chains since we can only convert from virtual to physical,
148  * not the other way around.  The size must track the free index arrays.
149  */
150 struct xn_chain_data {
151         struct mbuf             *xn_tx_chain[NET_TX_RING_SIZE+1];
152         struct mbuf             *xn_rx_chain[NET_RX_RING_SIZE+1];
153 };
154
155
156 struct net_device_stats
157 {
158         u_long  rx_packets;             /* total packets received       */
159         u_long  tx_packets;             /* total packets transmitted    */
160         u_long  rx_bytes;               /* total bytes received         */
161         u_long  tx_bytes;               /* total bytes transmitted      */
162         u_long  rx_errors;              /* bad packets received         */
163         u_long  tx_errors;              /* packet transmit problems     */
164         u_long  rx_dropped;             /* no space in linux buffers    */
165         u_long  tx_dropped;             /* no space available in linux  */
166         u_long  multicast;              /* multicast packets received   */
167         u_long  collisions;
168
169         /* detailed rx_errors: */
170         u_long  rx_length_errors;
171         u_long  rx_over_errors;         /* receiver ring buff overflow  */
172         u_long  rx_crc_errors;          /* recved pkt with crc error    */
173         u_long  rx_frame_errors;        /* recv'd frame alignment error */
174         u_long  rx_fifo_errors;         /* recv'r fifo overrun          */
175         u_long  rx_missed_errors;       /* receiver missed packet       */
176
177         /* detailed tx_errors */
178         u_long  tx_aborted_errors;
179         u_long  tx_carrier_errors;
180         u_long  tx_fifo_errors;
181         u_long  tx_heartbeat_errors;
182         u_long  tx_window_errors;
183         
184         /* for cslip etc */
185         u_long  rx_compressed;
186         u_long  tx_compressed;
187 };
188
189 struct netfront_info {
190                 
191         struct ifnet *xn_ifp;
192
193         struct net_device_stats stats;
194         u_int tx_full;
195
196         netif_tx_front_ring_t tx;
197         netif_rx_front_ring_t rx;
198
199         struct mtx   tx_lock;
200         struct mtx   rx_lock;
201         struct sx    sc_lock;
202
203         u_int handle;
204         u_int irq;
205         u_int copying_receiver;
206         u_int carrier;
207                 
208         /* Receive-ring batched refills. */
209 #define RX_MIN_TARGET 32
210 #define RX_MAX_TARGET NET_RX_RING_SIZE
211         int rx_min_target, rx_max_target, rx_target;
212
213         /*
214          * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
215          * array is an index into a chain of free entries.
216          */
217
218         grant_ref_t gref_tx_head;
219         grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1]; 
220         grant_ref_t gref_rx_head;
221         grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1]; 
222
223 #define TX_MAX_TARGET min(NET_RX_RING_SIZE, 256)
224         struct xenbus_device *xbdev;
225         int tx_ring_ref;
226         int rx_ring_ref;
227         uint8_t mac[ETHER_ADDR_LEN];
228         struct xn_chain_data    xn_cdata;       /* mbufs */
229         struct mbuf_head xn_rx_batch;   /* head of the batch queue */
230
231         int                     xn_if_flags;
232         struct callout          xn_stat_ch;
233
234         u_long rx_pfn_array[NET_RX_RING_SIZE];
235         multicall_entry_t rx_mcl[NET_RX_RING_SIZE+1];
236         mmu_update_t rx_mmu[NET_RX_RING_SIZE];
237 };
238
239 #define rx_mbufs xn_cdata.xn_rx_chain
240 #define tx_mbufs xn_cdata.xn_tx_chain
241
242 #define XN_LOCK_INIT(_sc, _name) \
243         mtx_init(&(_sc)->tx_lock, #_name"_tx", "network transmit lock", MTX_DEF); \
244         mtx_init(&(_sc)->rx_lock, #_name"_rx", "network receive lock", MTX_DEF);  \
245         sx_init(&(_sc)->sc_lock, #_name"_rx")
246
247 #define XN_RX_LOCK(_sc)           mtx_lock(&(_sc)->rx_lock)
248 #define XN_RX_UNLOCK(_sc)         mtx_unlock(&(_sc)->rx_lock)
249
250 #define XN_TX_LOCK(_sc)           mtx_lock(&(_sc)->tx_lock)
251 #define XN_TX_UNLOCK(_sc)         mtx_unlock(&(_sc)->tx_lock)
252
253 #define XN_LOCK(_sc)           sx_xlock(&(_sc)->sc_lock); 
254 #define XN_UNLOCK(_sc)         sx_xunlock(&(_sc)->sc_lock); 
255
256 #define XN_LOCK_ASSERT(_sc)    sx_assert(&(_sc)->sc_lock, SX_LOCKED); 
257 #define XN_RX_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->rx_lock, MA_OWNED); 
258 #define XN_TX_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->tx_lock, MA_OWNED); 
259 #define XN_LOCK_DESTROY(_sc)   mtx_destroy(&(_sc)->rx_lock); \
260                                mtx_destroy(&(_sc)->tx_lock); \
261                                sx_destroy(&(_sc)->sc_lock);
262
263 struct netfront_rx_info {
264         struct netif_rx_response rx;
265         struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
266 };
267
268 #define netfront_carrier_on(netif)      ((netif)->carrier = 1)
269 #define netfront_carrier_off(netif)     ((netif)->carrier = 0)
270 #define netfront_carrier_ok(netif)      ((netif)->carrier)
271
272 /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */
273
274
275
276 /*
277  * Access macros for acquiring freeing slots in tx_skbs[].
278  */
279
280 static inline void
281 add_id_to_freelist(struct mbuf **list, unsigned short id)
282 {
283         list[id] = list[0];
284         list[0]  = (void *)(u_long)id;
285 }
286
287 static inline unsigned short
288 get_id_from_freelist(struct mbuf **list)
289 {
290         u_int id = (u_int)(u_long)list[0];
291         list[0] = list[id];
292         return (id);
293 }
294
295 static inline int
296 xennet_rxidx(RING_IDX idx)
297 {
298         return idx & (NET_RX_RING_SIZE - 1);
299 }
300
301 static inline struct mbuf *
302 xennet_get_rx_mbuf(struct netfront_info *np,
303                                                 RING_IDX ri)
304 {
305         int i = xennet_rxidx(ri);
306         struct mbuf *m;
307
308         m = np->rx_mbufs[i];
309         np->rx_mbufs[i] = NULL;
310         return (m);
311 }
312
313 static inline grant_ref_t
314 xennet_get_rx_ref(struct netfront_info *np, RING_IDX ri)
315 {
316         int i = xennet_rxidx(ri);
317         grant_ref_t ref = np->grant_rx_ref[i];
318         np->grant_rx_ref[i] = GRANT_INVALID_REF;
319         return ref;
320 }
321
322 #ifdef DEBUG
323
324 #endif
325 #define IPRINTK(fmt, args...) \
326     printf("[XEN] " fmt, ##args)
327 #define WPRINTK(fmt, args...) \
328     printf("[XEN] " fmt, ##args)
329 #define DPRINTK(fmt, args...) \
330     printf("[XEN] " fmt, ##args)
331
332
333 static __inline struct mbuf* 
334 makembuf (struct mbuf *buf)
335 {
336         struct mbuf *m = NULL;
337         
338         MGETHDR (m, M_DONTWAIT, MT_DATA);
339         
340         if (! m)
341                 return 0;
342                 
343                 M_MOVE_PKTHDR(m, buf);
344
345                 m_cljget(m, M_DONTWAIT, MJUMPAGESIZE);
346         m->m_pkthdr.len = buf->m_pkthdr.len;
347         m->m_len = buf->m_len;
348                 m_copydata(buf, 0, buf->m_pkthdr.len, mtod(m,caddr_t) );
349
350                 m->m_ext.ext_arg1 = (caddr_t *)(uintptr_t)(vtophys(mtod(m,caddr_t)) >> PAGE_SHIFT);
351         
352         return m;
353 }
354
355 /**
356  * Read the 'mac' node at the given device's node in the store, and parse that
357  * as colon-separated octets, placing result the given mac array.  mac must be
358  * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h).
359  * Return 0 on success, or errno on error.
360  */
361 static int 
362 xen_net_read_mac(struct xenbus_device *dev, uint8_t mac[])
363 {
364         char *s;
365         int i;
366         char *e;
367         char *macstr = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
368         if (IS_ERR(macstr)) {
369                 return PTR_ERR(macstr);
370         }
371         s = macstr;
372         for (i = 0; i < ETHER_ADDR_LEN; i++) {
373                 mac[i] = strtoul(s, &e, 16);
374                 if (s == e || (e[0] != ':' && e[0] != 0)) {
375                         free(macstr, M_DEVBUF);
376                         return ENOENT;
377                 }
378                 s = &e[1];
379         }
380         free(macstr, M_DEVBUF);
381         return 0;
382 }
383
384 /**
385  * Entry point to this code when a new device is created.  Allocate the basic
386  * structures and the ring buffers for communication with the backend, and
387  * inform the backend of the appropriate details for those.  Switch to
388  * Connected state.
389  */
390 static int 
391 netfront_probe(struct xenbus_device *dev, const struct xenbus_device_id *id)
392 {
393         int err;
394         struct ifnet *ifp;
395         struct netfront_info *info;
396
397         printf("netfront_probe() \n");
398         
399         err = create_netdev(dev, &ifp);
400         if (err) {
401                 xenbus_dev_fatal(dev, err, "creating netdev");
402                 return err;
403         }
404
405         info = ifp->if_softc;
406         dev->dev_driver_data = info;
407
408         
409         return 0;
410 }
411
412
413 /**
414  * We are reconnecting to the backend, due to a suspend/resume, or a backend
415  * driver restart.  We tear down our netif structure and recreate it, but
416  * leave the device-layer structures intact so that this is transparent to the
417  * rest of the kernel.
418  */
419 static int 
420 netfront_resume(struct xenbus_device *dev)
421 {
422         struct netfront_info *info = dev->dev_driver_data;
423         
424         DPRINTK("%s\n", dev->nodename);
425         
426         netif_disconnect_backend(info);
427         return (0);
428 }
429
430
431 /* Common code used when first setting up, and when resuming. */
432 static int 
433 talk_to_backend(struct xenbus_device *dev, struct netfront_info *info)
434 {
435         const char *message;
436         struct xenbus_transaction xbt;
437         int err;
438
439         err = xen_net_read_mac(dev, info->mac);
440         if (err) {
441                 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
442                 goto out;
443         }
444
445         /* Create shared ring, alloc event channel. */
446         err = setup_device(dev, info);
447         if (err)
448                 goto out;
449         
450  again:
451         err = xenbus_transaction_start(&xbt);
452         if (err) {
453                 xenbus_dev_fatal(dev, err, "starting transaction");
454                 goto destroy_ring;
455         }
456         err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
457                             info->tx_ring_ref);
458         if (err) {
459                 message = "writing tx ring-ref";
460                 goto abort_transaction;
461         }
462         err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
463                             info->rx_ring_ref);
464         if (err) {
465                 message = "writing rx ring-ref";
466                 goto abort_transaction;
467         }
468         err = xenbus_printf(xbt, dev->nodename,
469                 "event-channel", "%u", irq_to_evtchn_port(info->irq));
470         if (err) {
471                 message = "writing event-channel";
472                 goto abort_transaction;
473         }
474         err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
475                             info->copying_receiver);
476         if (err) {
477                 message = "writing request-rx-copy";
478                 goto abort_transaction;
479         }
480         err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
481         if (err) {
482                 message = "writing feature-rx-notify";
483                 goto abort_transaction;
484         }
485         err = xenbus_printf(xbt, dev->nodename, "feature-no-csum-offload", "%d", 1);
486         if (err) {
487                 message = "writing feature-no-csum-offload";
488                 goto abort_transaction;
489         }
490         err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
491         if (err) {
492                 message = "writing feature-sg";
493                 goto abort_transaction;
494         }
495 #ifdef HAVE_TSO
496         err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
497         if (err) {
498                 message = "writing feature-gso-tcpv4";
499                 goto abort_transaction;
500         }
501 #endif
502
503         err = xenbus_transaction_end(xbt, 0);
504         if (err) {
505                 if (err == EAGAIN)
506                         goto again;
507                 xenbus_dev_fatal(dev, err, "completing transaction");
508                 goto destroy_ring;
509         }
510         
511         return 0;
512         
513  abort_transaction:
514         xenbus_transaction_end(xbt, 1);
515         xenbus_dev_fatal(dev, err, "%s", message);
516  destroy_ring:
517         netif_free(info);
518  out:
519         return err;
520 }
521
522
523 static int 
524 setup_device(struct xenbus_device *dev, struct netfront_info *info)
525 {
526         netif_tx_sring_t *txs;
527         netif_rx_sring_t *rxs;
528         int err;
529         struct ifnet *ifp;
530         
531         ifp = info->xn_ifp;
532
533         info->tx_ring_ref = GRANT_INVALID_REF;
534         info->rx_ring_ref = GRANT_INVALID_REF;
535         info->rx.sring = NULL;
536         info->tx.sring = NULL;
537         info->irq = 0;
538
539         txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
540         if (!txs) {
541                 err = ENOMEM;
542                 xenbus_dev_fatal(dev, err, "allocating tx ring page");
543                 goto fail;
544         }
545         SHARED_RING_INIT(txs);
546         FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
547         err = xenbus_grant_ring(dev, virt_to_mfn(txs));
548         if (err < 0)
549                 goto fail;
550         info->tx_ring_ref = err;
551
552         rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
553         if (!rxs) {
554                 err = ENOMEM;
555                 xenbus_dev_fatal(dev, err, "allocating rx ring page");
556                 goto fail;
557         }
558         SHARED_RING_INIT(rxs);
559         FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
560
561         err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
562         if (err < 0)
563                 goto fail;
564         info->rx_ring_ref = err;
565
566 #if 0   
567         network_connect(ifp);
568 #endif
569         err = bind_listening_port_to_irqhandler(dev->otherend_id,
570                 "xn", xn_intr, info, INTR_TYPE_NET | INTR_MPSAFE, NULL);
571
572         if (err <= 0) {
573                 xenbus_dev_fatal(dev, err,
574                                  "bind_evtchn_to_irqhandler failed");
575                 goto fail;
576         }
577         info->irq = err;
578         
579         show_device(info);
580         
581         return 0;
582         
583  fail:
584         netif_free(info);
585         return err;
586 }
587
588 /**
589  * Callback received when the backend's state changes.
590  */
591 static void
592 backend_changed(struct xenbus_device *dev,
593                             XenbusState backend_state)
594 {
595                 struct netfront_info *sc = dev->dev_driver_data;
596                 
597         DPRINTK("\n");
598         
599         switch (backend_state) {
600         case XenbusStateInitialising:
601         case XenbusStateInitialised:
602         case XenbusStateConnected:
603         case XenbusStateUnknown:
604         case XenbusStateClosed:
605                         break;
606         case XenbusStateInitWait:
607                 if (dev->state != XenbusStateInitialising)
608                         break;
609                 if (network_connect(sc->xn_ifp) != 0)
610                         break;
611                 xenbus_switch_state(dev, XenbusStateConnected);
612 #ifdef notyet           
613                 (void)send_fake_arp(netdev);
614 #endif          
615                 break;  break;
616         case XenbusStateClosing:
617                         xenbus_frontend_closed(dev);
618                 break;
619         }
620 }
621
622 static void
623 xn_free_rx_ring(struct netfront_info *sc)
624 {
625 #if 0
626         int i;
627         
628         for (i = 0; i < NET_RX_RING_SIZE; i++) {
629                 if (sc->xn_cdata.xn_rx_chain[i] != NULL) {
630                         m_freem(sc->xn_cdata.xn_rx_chain[i]);
631                         sc->xn_cdata.xn_rx_chain[i] = NULL;
632                 }
633         }
634         
635         sc->rx.rsp_cons = 0;
636         sc->xn_rx_if->req_prod = 0;
637         sc->xn_rx_if->event = sc->rx.rsp_cons ;
638 #endif
639 }
640
641 static void
642 xn_free_tx_ring(struct netfront_info *sc)
643 {
644 #if 0
645         int i;
646         
647         for (i = 0; i < NET_TX_RING_SIZE; i++) {
648                 if (sc->xn_cdata.xn_tx_chain[i] != NULL) {
649                         m_freem(sc->xn_cdata.xn_tx_chain[i]);
650                         sc->xn_cdata.xn_tx_chain[i] = NULL;
651                 }
652         }
653         
654         return;
655 #endif
656 }
657
658 static inline int
659 netfront_tx_slot_available(struct netfront_info *np)
660 {
661         return ((np->tx.req_prod_pvt - np->tx.rsp_cons) <
662                 (TX_MAX_TARGET - /* MAX_SKB_FRAGS */ 24 - 2));
663 }
664 static void
665 netif_release_tx_bufs(struct netfront_info *np)
666 {
667         struct mbuf *m;
668         int i;
669
670         for (i = 1; i <= NET_TX_RING_SIZE; i++) {
671                 m = np->xn_cdata.xn_tx_chain[i];
672
673                 if (((u_long)m) < KERNBASE)
674                         continue;
675                 gnttab_grant_foreign_access_ref(np->grant_tx_ref[i],
676                     np->xbdev->otherend_id, virt_to_mfn(mtod(m, vm_offset_t)),
677                     GNTMAP_readonly);
678                 gnttab_release_grant_reference(&np->gref_tx_head,
679                     np->grant_tx_ref[i]);
680                 np->grant_tx_ref[i] = GRANT_INVALID_REF;
681                 add_id_to_freelist(np->tx_mbufs, i);
682                 m_freem(m);
683         }
684 }
685
686 static void
687 network_alloc_rx_buffers(struct netfront_info *sc)
688 {
689         unsigned short id;
690         struct mbuf *m_new;
691         int i, batch_target, notify;
692         RING_IDX req_prod;
693         struct xen_memory_reservation reservation;
694         grant_ref_t ref;
695         int nr_flips;
696         netif_rx_request_t *req;
697         vm_offset_t vaddr;
698         u_long pfn;
699         
700         req_prod = sc->rx.req_prod_pvt;
701
702         if (unlikely(sc->carrier == 0))
703                 return;
704         
705         /*
706          * Allocate skbuffs greedily, even though we batch updates to the
707          * receive ring. This creates a less bursty demand on the memory
708          * allocator, so should reduce the chance of failed allocation
709          * requests both for ourself and for other kernel subsystems.
710          */
711         batch_target = sc->rx_target - (req_prod - sc->rx.rsp_cons);
712         for (i = mbufq_len(&sc->xn_rx_batch); i < batch_target; i++) {
713                 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
714                 if (m_new == NULL) 
715                         goto no_mbuf;
716
717                 m_cljget(m_new, M_DONTWAIT, MJUMPAGESIZE);
718                 if ((m_new->m_flags & M_EXT) == 0) {
719                         m_freem(m_new);
720
721 no_mbuf:
722                         if (i != 0)
723                                 goto refill;
724                         /*
725                          * XXX set timer
726                          */
727                         break;
728                 }
729                 m_new->m_len = m_new->m_pkthdr.len = MJUMPAGESIZE;
730                 
731                 /* queue the mbufs allocated */
732                 mbufq_tail(&sc->xn_rx_batch, m_new);
733         }
734         
735         /* Is the batch large enough to be worthwhile? */
736         if (i < (sc->rx_target/2)) {
737                 if (req_prod >sc->rx.sring->req_prod)
738                         goto push;
739                 return;
740         }
741         /* Adjust floating fill target if we risked running out of buffers. */
742         if ( ((req_prod - sc->rx.sring->rsp_prod) < (sc->rx_target / 4)) &&
743              ((sc->rx_target *= 2) > sc->rx_max_target) )
744                 sc->rx_target = sc->rx_max_target;
745
746 refill:
747         for (nr_flips = i = 0; ; i++) {
748                 if ((m_new = mbufq_dequeue(&sc->xn_rx_batch)) == NULL)
749                         break;
750
751                 m_new->m_ext.ext_arg1 = (vm_paddr_t *)(uintptr_t)(
752                     vtophys(m_new->m_ext.ext_buf) >> PAGE_SHIFT);
753
754                 id = xennet_rxidx(req_prod + i);
755
756                 KASSERT(sc->xn_cdata.xn_rx_chain[id] == NULL,
757                     ("non-NULL xm_rx_chain"));
758                 sc->xn_cdata.xn_rx_chain[id] = m_new;
759
760                 ref = gnttab_claim_grant_reference(&sc->gref_rx_head);
761                 KASSERT((short)ref >= 0, ("negative ref"));
762                 sc->grant_rx_ref[id] = ref;
763
764                 vaddr = mtod(m_new, vm_offset_t);
765                 pfn = vtophys(vaddr) >> PAGE_SHIFT;
766                 req = RING_GET_REQUEST(&sc->rx, req_prod + i);
767
768                 if (sc->copying_receiver == 0) {
769                         gnttab_grant_foreign_transfer_ref(ref,
770                             sc->xbdev->otherend_id, pfn);
771                         sc->rx_pfn_array[nr_flips] = PFNTOMFN(pfn);
772                         if (!xen_feature(XENFEAT_auto_translated_physmap)) {
773                                 /* Remove this page before passing
774                                  * back to Xen.
775                                  */
776                                 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
777                                 MULTI_update_va_mapping(&sc->rx_mcl[i],
778                                     vaddr, 0, 0);
779                         }
780                         nr_flips++;
781                 } else {
782                         gnttab_grant_foreign_access_ref(ref,
783                             sc->xbdev->otherend_id,
784                             PFNTOMFN(pfn), 0);
785                 }
786                 req->id = id;
787                 req->gref = ref;
788                 
789                 sc->rx_pfn_array[i] =
790                     vtomach(mtod(m_new,vm_offset_t)) >> PAGE_SHIFT;
791         } 
792         
793         KASSERT(i, ("no mbufs processed")); /* should have returned earlier */
794         KASSERT(mbufq_len(&sc->xn_rx_batch) == 0, ("not all mbufs processed"));
795         /*
796          * We may have allocated buffers which have entries outstanding
797          * in the page * update queue -- make sure we flush those first!
798          */
799         PT_UPDATES_FLUSH();
800         if (nr_flips != 0) {
801 #ifdef notyet
802                 /* Tell the ballon driver what is going on. */
803                 balloon_update_driver_allowance(i);
804 #endif
805                 set_xen_guest_handle(reservation.extent_start,sc->rx_pfn_array);
806                 reservation.nr_extents   = i;
807                 reservation.extent_order = 0;
808                 reservation.address_bits = 0;
809                 reservation.domid        = DOMID_SELF;
810
811                 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
812
813                         /* After all PTEs have been zapped, flush the TLB. */
814                         sc->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
815                             UVMF_TLB_FLUSH|UVMF_ALL;
816         
817                         /* Give away a batch of pages. */
818                         sc->rx_mcl[i].op = __HYPERVISOR_memory_op;
819                         sc->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
820                         sc->rx_mcl[i].args[1] =  (u_long)&reservation;
821                         /* Zap PTEs and give away pages in one big multicall. */
822                         (void)HYPERVISOR_multicall(sc->rx_mcl, i+1);
823
824                         /* Check return status of HYPERVISOR_dom_mem_op(). */
825                         if (unlikely(sc->rx_mcl[i].result != i))
826                                 panic("Unable to reduce memory reservation\n");
827                         } else {
828                                 if (HYPERVISOR_memory_op(
829                                     XENMEM_decrease_reservation, &reservation)
830                                     != i)
831                                         panic("Unable to reduce memory "
832                                             "reservation\n");
833                 }
834         } else {
835                 wmb();
836         }
837                         
838         /* Above is a suitable barrier to ensure backend will see requests. */
839         sc->rx.req_prod_pvt = req_prod + i;
840 push:
841         RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->rx, notify);
842         if (notify)
843                 notify_remote_via_irq(sc->irq);
844 }
845
846 static void
847 xn_rxeof(struct netfront_info *np)
848 {
849         struct ifnet *ifp;
850         struct netfront_rx_info rinfo;
851         struct netif_rx_response *rx = &rinfo.rx;
852         struct netif_extra_info *extras = rinfo.extras;
853         RING_IDX i, rp;
854         multicall_entry_t *mcl;
855         struct mbuf *m;
856         struct mbuf_head rxq, errq, tmpq;
857         int err, pages_flipped = 0;
858
859         XN_RX_LOCK_ASSERT(np);
860         if (!netfront_carrier_ok(np))
861                 return;
862
863         mbufq_init(&tmpq);
864         mbufq_init(&errq);
865         mbufq_init(&rxq);
866
867         ifp = np->xn_ifp;
868         
869         rp = np->rx.sring->rsp_prod;
870         rmb();  /* Ensure we see queued responses up to 'rp'. */
871
872         i = np->rx.rsp_cons;
873         while ((i != rp)) {
874                 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
875                 memset(extras, 0, sizeof(rinfo.extras));
876
877                 err = xennet_get_responses(np, &rinfo, rp, &tmpq,
878                     &pages_flipped);
879
880                 if (unlikely(err)) {
881                         while ((m = mbufq_dequeue(&tmpq)))
882                                 mbufq_tail(&errq, m);
883                         np->stats.rx_errors++;
884                         i = np->rx.rsp_cons;
885                         continue;
886                 }
887
888                 m = mbufq_dequeue(&tmpq);
889
890                 m->m_data += rx->offset;/* (rx->addr & PAGE_MASK); */
891                 m->m_pkthdr.len = m->m_len = rx->status;
892                 m->m_pkthdr.rcvif = ifp;
893
894                 if ( rx->flags & NETRXF_data_validated ) {
895                         /* Tell the stack the checksums are okay */
896                         /*
897                          * XXX this isn't necessarily the case - need to add
898                          * check
899                          */
900                                 
901                         m->m_pkthdr.csum_flags |=
902                             (CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_DATA_VALID
903                             | CSUM_PSEUDO_HDR);
904                         m->m_pkthdr.csum_data = 0xffff;
905                 }
906
907                 np->stats.rx_packets++;
908                 np->stats.rx_bytes += rx->status;
909
910                 mbufq_tail(&rxq, m);
911                 np->rx.rsp_cons = ++i;
912         }
913
914         if (pages_flipped) {
915                 /* Some pages are no longer absent... */
916 #ifdef notyet
917                 balloon_update_driver_allowance(-pages_flipped);
918 #endif
919                 /* Do all the remapping work, and M->P updates, in one big
920                  * hypercall.
921                  */
922                 if (!!xen_feature(XENFEAT_auto_translated_physmap)) {
923                         mcl = np->rx_mcl + pages_flipped;
924                         mcl->op = __HYPERVISOR_mmu_update;
925                         mcl->args[0] = (u_long)np->rx_mmu;
926                         mcl->args[1] = pages_flipped;
927                         mcl->args[2] = 0;
928                         mcl->args[3] = DOMID_SELF;
929                         (void)HYPERVISOR_multicall(np->rx_mcl,
930                             pages_flipped + 1);
931                 }
932         }
933         
934         while ((m = mbufq_dequeue(&errq)))
935                 m_freem(m);
936
937         /* 
938          * Process all the mbufs after the remapping is complete.
939          * Break the mbuf chain first though.
940          */
941         while ((m = mbufq_dequeue(&rxq)) != NULL) {
942                 ifp->if_ipackets++;
943                         
944                 /*
945                  * Do we really need to drop the rx lock?
946                  */
947                 XN_RX_UNLOCK(np);
948                 /* Pass it up. */
949                 (*ifp->if_input)(ifp, m);
950                 XN_RX_LOCK(np);
951         }
952         
953         np->rx.rsp_cons = i;
954
955 #if 0
956         /* If we get a callback with very few responses, reduce fill target. */
957         /* NB. Note exponential increase, linear decrease. */
958         if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) > 
959             ((3*np->rx_target) / 4)) && (--np->rx_target < np->rx_min_target))
960                 np->rx_target = np->rx_min_target;
961 #endif
962         
963         network_alloc_rx_buffers(np);
964
965         np->rx.sring->rsp_event = i + 1;
966 }
967
968 static void 
969 xn_txeof(struct netfront_info *np)
970 {
971         RING_IDX i, prod;
972         unsigned short id;
973         struct ifnet *ifp;
974         struct mbuf *m;
975         
976         XN_TX_LOCK_ASSERT(np);
977         
978         if (!netfront_carrier_ok(np))
979                 return;
980         
981         ifp = np->xn_ifp;
982         ifp->if_timer = 0;
983         
984         do {
985                 prod = np->tx.sring->rsp_prod;
986                 rmb(); /* Ensure we see responses up to 'rp'. */
987                 
988                 for (i = np->tx.rsp_cons; i != prod; i++) {
989                         id = RING_GET_RESPONSE(&np->tx, i)->id;
990                         m = np->xn_cdata.xn_tx_chain[id]; 
991                         
992                         ifp->if_opackets++;
993                         KASSERT(m != NULL, ("mbuf not found in xn_tx_chain"));
994                         M_ASSERTVALID(m);
995                         if (unlikely(gnttab_query_foreign_access(
996                             np->grant_tx_ref[id]) != 0)) {
997                                 printf("network_tx_buf_gc: warning "
998                                     "-- grant still in use by backend "
999                                     "domain.\n");
1000                                 goto out; 
1001                         }
1002                         gnttab_end_foreign_access_ref(
1003                                 np->grant_tx_ref[id], GNTMAP_readonly);
1004                         gnttab_release_grant_reference(
1005                                 &np->gref_tx_head, np->grant_tx_ref[id]);
1006                         np->grant_tx_ref[id] = GRANT_INVALID_REF;
1007                         
1008                         np->xn_cdata.xn_tx_chain[id] = NULL;
1009                         add_id_to_freelist(np->xn_cdata.xn_tx_chain, id);
1010                         m_freem(m);
1011                 }
1012                 np->tx.rsp_cons = prod;
1013                 
1014                 /*
1015                  * Set a new event, then check for race with update of
1016                  * tx_cons. Note that it is essential to schedule a
1017                  * callback, no matter how few buffers are pending. Even if
1018                  * there is space in the transmit ring, higher layers may
1019                  * be blocked because too much data is outstanding: in such
1020                  * cases notification from Xen is likely to be the only kick
1021                  * that we'll get.
1022                  */
1023                 np->tx.sring->rsp_event =
1024                     prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
1025
1026                 mb();
1027                 
1028         } while (prod != np->tx.sring->rsp_prod);
1029         
1030  out: 
1031         if (np->tx_full &&
1032             ((np->tx.sring->req_prod - prod) < NET_TX_RING_SIZE)) {
1033                 np->tx_full = 0;
1034 #if 0
1035                 if (np->user_state == UST_OPEN)
1036                         netif_wake_queue(dev);
1037 #endif
1038         }
1039
1040 }
1041
1042 static void
1043 xn_intr(void *xsc)
1044 {
1045         struct netfront_info *np = xsc;
1046         struct ifnet *ifp = np->xn_ifp;
1047
1048 #if 0
1049         if (!(np->rx.rsp_cons != np->rx.sring->rsp_prod &&
1050             likely(netfront_carrier_ok(np)) &&
1051             ifp->if_drv_flags & IFF_DRV_RUNNING))
1052                 return;
1053 #endif
1054         if (np->tx.rsp_cons != np->tx.sring->rsp_prod) {
1055                 XN_TX_LOCK(np);
1056                 xn_txeof(np);
1057                 XN_TX_UNLOCK(np);                       
1058         }       
1059
1060         XN_RX_LOCK(np);
1061         xn_rxeof(np);
1062         XN_RX_UNLOCK(np);
1063
1064         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1065             !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1066                 xn_start(ifp);
1067 }
1068
1069
1070 static void
1071 xennet_move_rx_slot(struct netfront_info *np, struct mbuf *m,
1072         grant_ref_t ref)
1073 {
1074         int new = xennet_rxidx(np->rx.req_prod_pvt);
1075
1076         KASSERT(np->rx_mbufs[new] == NULL, ("rx_mbufs != NULL"));
1077         np->rx_mbufs[new] = m;
1078         np->grant_rx_ref[new] = ref;
1079         RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
1080         RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
1081         np->rx.req_prod_pvt++;
1082 }
1083
1084 static int
1085 xennet_get_extras(struct netfront_info *np,
1086     struct netif_extra_info *extras, RING_IDX rp)
1087 {
1088         struct netif_extra_info *extra;
1089         RING_IDX cons = np->rx.rsp_cons;
1090
1091         int err = 0;
1092
1093         do {
1094                 struct mbuf *m;
1095                 grant_ref_t ref;
1096
1097                 if (unlikely(cons + 1 == rp)) {
1098 #if 0                   
1099                         if (net_ratelimit())
1100                                 WPRINTK("Missing extra info\n");
1101 #endif                  
1102                         err = -EINVAL;
1103                         break;
1104                 }
1105
1106                 extra = (struct netif_extra_info *)
1107                 RING_GET_RESPONSE(&np->rx, ++cons);
1108
1109                 if (unlikely(!extra->type ||
1110                         extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1111 #if 0                           
1112                         if (net_ratelimit())
1113                                 WPRINTK("Invalid extra type: %d\n",
1114                                         extra->type);
1115 #endif                  
1116                         err = -EINVAL;
1117                 } else {
1118                         memcpy(&extras[extra->type - 1], extra, sizeof(*extra));
1119                 }
1120
1121                 m = xennet_get_rx_mbuf(np, cons);
1122                 ref = xennet_get_rx_ref(np, cons);
1123                 xennet_move_rx_slot(np, m, ref);
1124         } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
1125
1126         np->rx.rsp_cons = cons;
1127         return err;
1128 }
1129
1130 static int
1131 xennet_get_responses(struct netfront_info *np,
1132         struct netfront_rx_info *rinfo, RING_IDX rp,
1133         struct mbuf_head *list,
1134         int *pages_flipped_p)
1135 {
1136         int pages_flipped = *pages_flipped_p;
1137         struct mmu_update *mmu;
1138         struct multicall_entry *mcl;
1139         struct netif_rx_response *rx = &rinfo->rx;
1140         struct netif_extra_info *extras = rinfo->extras;
1141         RING_IDX cons = np->rx.rsp_cons;
1142         struct mbuf *m = xennet_get_rx_mbuf(np, cons);
1143         grant_ref_t ref = xennet_get_rx_ref(np, cons);
1144         int max = 24 /* MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD) */;
1145         int frags = 1;
1146         int err = 0;
1147         u_long ret;
1148
1149         if (rx->flags & NETRXF_extra_info) {
1150                 err = xennet_get_extras(np, extras, rp);
1151                 cons = np->rx.rsp_cons;
1152         }
1153
1154         for (;;) {
1155                 u_long mfn;
1156
1157                 if (unlikely(rx->status < 0 ||
1158                         rx->offset + rx->status > PAGE_SIZE)) {
1159 #if 0                                           
1160                         if (net_ratelimit())
1161                                 WPRINTK("rx->offset: %x, size: %u\n",
1162                                         rx->offset, rx->status);
1163 #endif                                          
1164                         xennet_move_rx_slot(np, m, ref);
1165                         err = -EINVAL;
1166                         goto next;
1167                 }
1168
1169                 /*
1170                  * This definitely indicates a bug, either in this driver or in
1171                  * the backend driver. In future this should flag the bad
1172                  * situation to the system controller to reboot the backed.
1173                  */
1174                 if (ref == GRANT_INVALID_REF) {
1175 #if 0                           
1176                         if (net_ratelimit())
1177                                 WPRINTK("Bad rx response id %d.\n", rx->id);
1178 #endif                  
1179                         err = -EINVAL;
1180                         goto next;
1181                 }
1182
1183                 if (!np->copying_receiver) {
1184                         /* Memory pressure, insufficient buffer
1185                          * headroom, ...
1186                          */
1187                         if (!(mfn = gnttab_end_foreign_transfer_ref(ref))) {
1188                                 if (net_ratelimit())
1189                                         WPRINTK("Unfulfilled rx req "
1190                                                 "(id=%d, st=%d).\n",
1191                                                 rx->id, rx->status);
1192                                 xennet_move_rx_slot(np, m, ref);
1193                                 err = -ENOMEM;
1194                                 goto next;
1195                         }
1196
1197                         if (!xen_feature( XENFEAT_auto_translated_physmap)) {
1198                                 /* Remap the page. */
1199                                 void *vaddr = mtod(m, void *);
1200                                 uint32_t pfn;
1201
1202                                 mcl = np->rx_mcl + pages_flipped;
1203                                 mmu = np->rx_mmu + pages_flipped;
1204
1205                                 MULTI_update_va_mapping(mcl, (u_long)vaddr,
1206                                     (mfn << PAGE_SHIFT) | PG_RW |
1207                                     PG_V | PG_M | PG_A, 0);
1208                                 pfn = (uint32_t)m->m_ext.ext_arg1;
1209                                 mmu->ptr = ((vm_paddr_t)mfn << PAGE_SHIFT) |
1210                                     MMU_MACHPHYS_UPDATE;
1211                                 mmu->val = pfn;
1212
1213                                 set_phys_to_machine(pfn, mfn);
1214                         }
1215                         pages_flipped++;
1216                 } else {
1217                         ret = gnttab_end_foreign_access_ref(ref, 0);
1218                         KASSERT(ret, ("ret != 0"));
1219                 }
1220
1221                 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1222                 mbufq_tail(list, m);
1223
1224 next:
1225                 if (!(rx->flags & NETRXF_more_data))
1226                         break;
1227
1228                 if (cons + frags == rp) {
1229                         if (net_ratelimit())
1230                                 WPRINTK("Need more frags\n");
1231                         err = -ENOENT;
1232                                 break;
1233                 }
1234
1235                 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
1236                 m = xennet_get_rx_mbuf(np, cons + frags);
1237                 ref = xennet_get_rx_ref(np, cons + frags);
1238                 frags++;
1239         }
1240
1241         if (unlikely(frags > max)) {
1242                 if (net_ratelimit())
1243                         WPRINTK("Too many frags\n");
1244                 err = -E2BIG;
1245         }
1246
1247         if (unlikely(err))
1248                 np->rx.rsp_cons = cons + frags;
1249
1250         *pages_flipped_p = pages_flipped;
1251
1252         return err;
1253 }
1254
1255 static void
1256 xn_tick_locked(struct netfront_info *sc) 
1257 {
1258         XN_RX_LOCK_ASSERT(sc);
1259         callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc);
1260
1261         /* XXX placeholder for printing debug information */
1262      
1263 }
1264
1265
1266 static void
1267 xn_tick(void *xsc) 
1268 {
1269         struct netfront_info *sc;
1270     
1271         sc = xsc;
1272         XN_RX_LOCK(sc);
1273         xn_tick_locked(sc);
1274         XN_RX_UNLOCK(sc);
1275      
1276 }
1277 static void
1278 xn_start_locked(struct ifnet *ifp) 
1279 {
1280         unsigned short id;
1281         struct mbuf *m_head, *new_m;
1282         struct netfront_info *sc;
1283         netif_tx_request_t *tx;
1284         RING_IDX i;
1285         grant_ref_t ref;
1286         u_long mfn, tx_bytes;
1287         int notify;
1288
1289         sc = ifp->if_softc;
1290         tx_bytes = 0;
1291
1292         if (!netfront_carrier_ok(sc))
1293                 return;
1294         
1295         for (i = sc->tx.req_prod_pvt; TRUE; i++) {
1296                 IF_DEQUEUE(&ifp->if_snd, m_head);
1297                 if (m_head == NULL) 
1298                         break;
1299                 
1300                 if (!netfront_tx_slot_available(sc)) {
1301                         IF_PREPEND(&ifp->if_snd, m_head);
1302                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1303                         break;
1304                 }
1305                 
1306                 id = get_id_from_freelist(sc->xn_cdata.xn_tx_chain);
1307
1308                 /*
1309                  * Start packing the mbufs in this chain into
1310                  * the fragment pointers. Stop when we run out
1311                  * of fragments or hit the end of the mbuf chain.
1312                  */
1313                 new_m = makembuf(m_head);
1314                 tx = RING_GET_REQUEST(&sc->tx, i);
1315                 tx->id = id;
1316                 ref = gnttab_claim_grant_reference(&sc->gref_tx_head);
1317                 KASSERT((short)ref >= 0, ("Negative ref"));
1318                 mfn = virt_to_mfn(mtod(new_m, vm_offset_t));
1319                 gnttab_grant_foreign_access_ref(ref, sc->xbdev->otherend_id,
1320                     mfn, GNTMAP_readonly);
1321                 tx->gref = sc->grant_tx_ref[id] = ref;
1322                 tx->size = new_m->m_pkthdr.len;
1323 #if 0
1324                 tx->flags = (skb->ip_summed == CHECKSUM_HW) ? NETTXF_csum_blank : 0;
1325 #endif
1326                 tx->flags = 0;
1327                 new_m->m_next = NULL;
1328                 new_m->m_nextpkt = NULL;
1329
1330                 m_freem(m_head);
1331
1332                 sc->xn_cdata.xn_tx_chain[id] = new_m;
1333                 BPF_MTAP(ifp, new_m);
1334
1335                 sc->stats.tx_bytes += new_m->m_pkthdr.len;
1336                 sc->stats.tx_packets++;
1337         }
1338
1339         sc->tx.req_prod_pvt = i;
1340         RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->tx, notify);
1341         if (notify)
1342                 notify_remote_via_irq(sc->irq);
1343
1344         xn_txeof(sc);
1345
1346         if (RING_FULL(&sc->tx)) {
1347                 sc->tx_full = 1;
1348 #if 0
1349                 netif_stop_queue(dev);
1350 #endif
1351         }
1352
1353         return;
1354 }    
1355
1356 static void
1357 xn_start(struct ifnet *ifp)
1358 {
1359         struct netfront_info *sc;
1360         sc = ifp->if_softc;
1361         XN_TX_LOCK(sc);
1362         xn_start_locked(ifp);
1363         XN_TX_UNLOCK(sc);
1364 }
1365
1366 /* equivalent of network_open() in Linux */
1367 static void 
1368 xn_ifinit_locked(struct netfront_info *sc) 
1369 {
1370         struct ifnet *ifp;
1371         
1372         XN_LOCK_ASSERT(sc);
1373         
1374         ifp = sc->xn_ifp;
1375         
1376         if (ifp->if_drv_flags & IFF_DRV_RUNNING) 
1377                 return;
1378         
1379         xn_stop(sc);
1380         
1381         network_alloc_rx_buffers(sc);
1382         sc->rx.sring->rsp_event = sc->rx.rsp_cons + 1;
1383         
1384         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1385         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1386         
1387         callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc);
1388
1389 }
1390
1391
1392 static void 
1393 xn_ifinit(void *xsc)
1394 {
1395         struct netfront_info *sc = xsc;
1396     
1397         XN_LOCK(sc);
1398         xn_ifinit_locked(sc);
1399         XN_UNLOCK(sc);
1400
1401 }
1402
1403
1404 static int
1405 xn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1406 {
1407         struct netfront_info *sc = ifp->if_softc;
1408         struct ifreq *ifr = (struct ifreq *) data;
1409         struct ifaddr *ifa = (struct ifaddr *)data;
1410
1411         int mask, error = 0;
1412         switch(cmd) {
1413         case SIOCSIFADDR:
1414         case SIOCGIFADDR:
1415                 XN_LOCK(sc);
1416                 if (ifa->ifa_addr->sa_family == AF_INET) {
1417                         ifp->if_flags |= IFF_UP;
1418                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 
1419                                 xn_ifinit_locked(sc);
1420                         arp_ifinit(ifp, ifa);
1421                 } else
1422                         error = ether_ioctl(ifp, cmd, data);
1423                 XN_UNLOCK(sc);
1424                 break;
1425         case SIOCSIFMTU:
1426                 /* XXX can we alter the MTU on a VN ?*/
1427 #ifdef notyet
1428                 if (ifr->ifr_mtu > XN_JUMBO_MTU)
1429                         error = EINVAL;
1430                 else 
1431 #endif
1432                 {
1433                         ifp->if_mtu = ifr->ifr_mtu;
1434                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1435                         xn_ifinit(sc);
1436                 }
1437                 break;
1438         case SIOCSIFFLAGS:
1439                 XN_LOCK(sc);
1440                 if (ifp->if_flags & IFF_UP) {
1441                         /*
1442                          * If only the state of the PROMISC flag changed,
1443                          * then just use the 'set promisc mode' command
1444                          * instead of reinitializing the entire NIC. Doing
1445                          * a full re-init means reloading the firmware and
1446                          * waiting for it to start up, which may take a
1447                          * second or two.
1448                          */
1449 #ifdef notyet
1450                         /* No promiscuous mode with Xen */
1451                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1452                             ifp->if_flags & IFF_PROMISC &&
1453                             !(sc->xn_if_flags & IFF_PROMISC)) {
1454                                 XN_SETBIT(sc, XN_RX_MODE,
1455                                           XN_RXMODE_RX_PROMISC);
1456                         } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1457                                    !(ifp->if_flags & IFF_PROMISC) &&
1458                                    sc->xn_if_flags & IFF_PROMISC) {
1459                                 XN_CLRBIT(sc, XN_RX_MODE,
1460                                           XN_RXMODE_RX_PROMISC);
1461                         } else
1462 #endif
1463                                 xn_ifinit_locked(sc);
1464                 } else {
1465                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1466                                 xn_stop(sc);
1467                         }
1468                 }
1469                 sc->xn_if_flags = ifp->if_flags;
1470                 XN_UNLOCK(sc);
1471                 error = 0;
1472                 break;
1473         case SIOCSIFCAP:
1474                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1475                 if (mask & IFCAP_HWCSUM) {
1476                         if (IFCAP_HWCSUM & ifp->if_capenable)
1477                                 ifp->if_capenable &= ~IFCAP_HWCSUM;
1478                         else
1479                                 ifp->if_capenable |= IFCAP_HWCSUM;
1480                 }
1481                 error = 0;
1482                 break;
1483         case SIOCADDMULTI:
1484         case SIOCDELMULTI:
1485 #ifdef notyet
1486                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1487                         XN_LOCK(sc);
1488                         xn_setmulti(sc);
1489                         XN_UNLOCK(sc);
1490                         error = 0;
1491                 }
1492 #endif
1493                 /* FALLTHROUGH */
1494         case SIOCSIFMEDIA:
1495         case SIOCGIFMEDIA:
1496                 error = EINVAL;
1497                 break;
1498         default:
1499                 error = ether_ioctl(ifp, cmd, data);
1500         }
1501     
1502         return (error);
1503 }
1504
1505 static void
1506 xn_stop(struct netfront_info *sc)
1507 {       
1508         struct ifnet *ifp;
1509
1510         XN_LOCK_ASSERT(sc);
1511     
1512         ifp = sc->xn_ifp;
1513
1514         callout_stop(&sc->xn_stat_ch);
1515
1516         xn_free_rx_ring(sc);
1517         xn_free_tx_ring(sc);
1518     
1519         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1520 }
1521
1522 /* START of Xenolinux helper functions adapted to FreeBSD */
1523 static int
1524 network_connect(struct ifnet *ifp)
1525 {
1526         struct netfront_info *np;
1527         int i, requeue_idx, err;
1528         grant_ref_t ref;
1529         netif_rx_request_t *req;
1530         u_int feature_rx_copy, feature_rx_flip;
1531
1532         printf("network_connect\n");
1533         
1534         np = ifp->if_softc;
1535         err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1536                            "feature-rx-copy", "%u", &feature_rx_copy);
1537         if (err != 1)
1538                 feature_rx_copy = 0;
1539         err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1540                            "feature-rx-flip", "%u", &feature_rx_flip);
1541         if (err != 1)
1542                 feature_rx_flip = 1;
1543
1544         /*
1545          * Copy packets on receive path if:
1546          *  (a) This was requested by user, and the backend supports it; or
1547          *  (b) Flipping was requested, but this is unsupported by the backend.
1548          */
1549         np->copying_receiver = ((MODPARM_rx_copy && feature_rx_copy) ||
1550                                 (MODPARM_rx_flip && !feature_rx_flip));
1551
1552         XN_LOCK(np);
1553         /* Recovery procedure: */
1554         err = talk_to_backend(np->xbdev, np);
1555         if (err) 
1556                         return (err);
1557         
1558         /* Step 1: Reinitialise variables. */
1559         netif_release_tx_bufs(np);
1560
1561         /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1562         for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1563                 struct mbuf *m;
1564
1565                 if (np->rx_mbufs[i] == NULL)
1566                         continue;
1567
1568                 m = np->rx_mbufs[requeue_idx] = xennet_get_rx_mbuf(np, i);
1569                 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1570                 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1571
1572                 if (!np->copying_receiver) {
1573                         gnttab_grant_foreign_transfer_ref(ref,
1574                             np->xbdev->otherend_id,
1575                             vtophys(mtod(m, vm_offset_t)));
1576                 } else {
1577                         gnttab_grant_foreign_access_ref(ref,
1578                             np->xbdev->otherend_id,
1579                             vtophys(mtod(m, vm_offset_t)), 0);
1580                 }
1581                 req->gref = ref;
1582                 req->id   = requeue_idx;
1583
1584                 requeue_idx++;
1585         }
1586
1587         np->rx.req_prod_pvt = requeue_idx;
1588         
1589         /* Step 3: All public and private state should now be sane.  Get
1590          * ready to start sending and receiving packets and give the driver
1591          * domain a kick because we've probably just requeued some
1592          * packets.
1593          */
1594         netfront_carrier_on(np);
1595         notify_remote_via_irq(np->irq);
1596         XN_TX_LOCK(np);
1597         xn_txeof(np);
1598         XN_TX_UNLOCK(np);
1599         network_alloc_rx_buffers(np);
1600         XN_UNLOCK(np);
1601
1602         return (0);
1603 }
1604
1605
1606 static void 
1607 show_device(struct netfront_info *sc)
1608 {
1609 #ifdef DEBUG
1610         if (sc) {
1611                 IPRINTK("<vif handle=%u %s(%s) evtchn=%u irq=%u tx=%p rx=%p>\n",
1612                         sc->xn_ifno,
1613                         be_state_name[sc->xn_backend_state],
1614                         sc->xn_user_state ? "open" : "closed",
1615                         sc->xn_evtchn,
1616                         sc->xn_irq,
1617                         sc->xn_tx_if,
1618                         sc->xn_rx_if);
1619         } else {
1620                 IPRINTK("<vif NULL>\n");
1621         }
1622 #endif
1623 }
1624
1625 static int ifno = 0;
1626
1627 /** Create a network device.
1628  * @param handle device handle
1629  */
1630 static int 
1631 create_netdev(struct xenbus_device *dev, struct ifnet **ifpp)
1632 {
1633         int i;
1634         struct netfront_info *np;
1635         int err;
1636         struct ifnet *ifp;
1637
1638         np = (struct netfront_info *)malloc(sizeof(struct netfront_info),
1639             M_DEVBUF, M_NOWAIT);
1640         if (np == NULL)
1641                         return (ENOMEM);
1642         
1643         memset(np, 0, sizeof(struct netfront_info));
1644         
1645         np->xbdev         = dev;
1646     
1647         XN_LOCK_INIT(np, xennetif);
1648         np->rx_target     = RX_MIN_TARGET;
1649         np->rx_min_target = RX_MIN_TARGET;
1650         np->rx_max_target = RX_MAX_TARGET;
1651         
1652         /* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
1653         for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1654                 np->tx_mbufs[i] = (void *) ((u_long) i+1);
1655                 np->grant_tx_ref[i] = GRANT_INVALID_REF;        
1656         }
1657         for (i = 0; i <= NET_RX_RING_SIZE; i++) {
1658                 np->rx_mbufs[i] = NULL;
1659                 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1660         }
1661         /* A grant for every tx ring slot */
1662         if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1663                                           &np->gref_tx_head) < 0) {
1664                 printf("#### netfront can't alloc tx grant refs\n");
1665                 err = ENOMEM;
1666                 goto exit;
1667         }
1668         /* A grant for every rx ring slot */
1669         if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1670                                           &np->gref_rx_head) < 0) {
1671                 printf("#### netfront can't alloc rx grant refs\n");
1672                 gnttab_free_grant_references(np->gref_tx_head);
1673                 err = ENOMEM;
1674                 goto exit;
1675         }
1676         
1677         err = xen_net_read_mac(dev, np->mac);
1678         if (err) {
1679                 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1680                 goto out;
1681         }
1682         
1683         /* Set up ifnet structure */
1684         *ifpp = ifp = np->xn_ifp = if_alloc(IFT_ETHER);
1685         ifp->if_softc = np;
1686         if_initname(ifp, "xn",  ifno++/* ifno */);
1687         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
1688         ifp->if_ioctl = xn_ioctl;
1689         ifp->if_output = ether_output;
1690         ifp->if_start = xn_start;
1691 #ifdef notyet
1692         ifp->if_watchdog = xn_watchdog;
1693 #endif
1694         ifp->if_init = xn_ifinit;
1695         ifp->if_mtu = ETHERMTU;
1696         ifp->if_snd.ifq_maxlen = NET_TX_RING_SIZE - 1;
1697         
1698 #ifdef notyet
1699         ifp->if_hwassist = XN_CSUM_FEATURES;
1700         ifp->if_capabilities = IFCAP_HWCSUM;
1701         ifp->if_capenable = ifp->if_capabilities;
1702 #endif    
1703         
1704         ether_ifattach(ifp, np->mac);
1705         callout_init(&np->xn_stat_ch, CALLOUT_MPSAFE);
1706         netfront_carrier_off(np);
1707
1708         return (0);
1709
1710 exit:
1711         gnttab_free_grant_references(np->gref_tx_head);
1712 out:
1713         panic("do something smart");
1714
1715 }
1716
1717 /**
1718  * Handle the change of state of the backend to Closing.  We must delete our
1719  * device-layer structures now, to ensure that writes are flushed through to
1720  * the backend.  Once is this done, we can switch to Closed in
1721  * acknowledgement.
1722  */
1723 #if 0
1724 static void netfront_closing(struct xenbus_device *dev)
1725 {
1726 #if 0
1727         struct netfront_info *info = dev->dev_driver_data;
1728
1729         DPRINTK("netfront_closing: %s removed\n", dev->nodename);
1730
1731         close_netdev(info);
1732 #endif
1733         xenbus_switch_state(dev, XenbusStateClosed);
1734 }
1735 #endif
1736
1737 static int netfront_remove(struct xenbus_device *dev)
1738 {
1739         struct netfront_info *info = dev->dev_driver_data;
1740
1741         DPRINTK("%s\n", dev->nodename);
1742
1743         netif_free(info);
1744         free(info, M_DEVBUF);
1745
1746         return 0;
1747 }
1748
1749
1750 static void netif_free(struct netfront_info *info)
1751 {
1752         netif_disconnect_backend(info);
1753 #if 0
1754         close_netdev(info);
1755 #endif
1756 }
1757
1758
1759
1760 static void netif_disconnect_backend(struct netfront_info *info)
1761 {
1762         xn_stop(info);
1763         end_access(info->tx_ring_ref, info->tx.sring);
1764         end_access(info->rx_ring_ref, info->rx.sring);
1765         info->tx_ring_ref = GRANT_INVALID_REF;
1766         info->rx_ring_ref = GRANT_INVALID_REF;
1767         info->tx.sring = NULL;
1768         info->rx.sring = NULL;
1769
1770 #if 0
1771         if (info->irq)
1772                 unbind_from_irqhandler(info->irq, info->netdev);
1773 #else 
1774         panic("FIX ME");
1775 #endif
1776         info->irq = 0;
1777 }
1778
1779
1780 static void end_access(int ref, void *page)
1781 {
1782         if (ref != GRANT_INVALID_REF)
1783                 gnttab_end_foreign_access(ref, 0, page);
1784 }
1785
1786
1787 /* ** Driver registration ** */
1788
1789
1790 static struct xenbus_device_id netfront_ids[] = {
1791         { "vif" },
1792         { "" }
1793 };
1794
1795
1796 static struct xenbus_driver netfront = {
1797         .name = "vif",
1798         .ids = netfront_ids,
1799         .probe = netfront_probe,
1800         .remove = netfront_remove,
1801         .resume = netfront_resume,
1802         .otherend_changed = backend_changed,
1803 };
1804
1805 static void
1806 netif_init(void *unused)
1807 {
1808         if (!is_running_on_xen())
1809                 return;
1810
1811         if (is_initial_xendomain())
1812                 return;
1813
1814         IPRINTK("Initialising virtual ethernet driver.\n");
1815
1816         xenbus_register_frontend(&netfront);
1817 }
1818
1819 SYSINIT(xennetif, SI_SUB_PSEUDO, SI_ORDER_SECOND, netif_init, NULL)
1820
1821 /*
1822  * Local variables:
1823  * mode: C
1824  * c-set-style: "BSD"
1825  * c-basic-offset: 8
1826  * tab-width: 4
1827  * indent-tabs-mode: t
1828  * End:
1829  */