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