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