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