1 /* $OpenBSD: vnet.c,v 1.67 2023/03/09 10:29:04 claudio Exp $ */ 2 /* 3 * Copyright (c) 2009, 2015 Mark Kettenis 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include "bpfilter.h" 19 20 #include <sys/param.h> 21 #include <sys/atomic.h> 22 #include <sys/device.h> 23 #include <sys/malloc.h> 24 #include <sys/pool.h> 25 #include <sys/mbuf.h> 26 #include <sys/socket.h> 27 #include <sys/sockio.h> 28 #include <sys/systm.h> 29 #include <sys/timeout.h> 30 31 #include <machine/autoconf.h> 32 #include <machine/hypervisor.h> 33 #include <machine/openfirm.h> 34 35 #include <net/if.h> 36 #include <net/if_media.h> 37 38 #include <netinet/in.h> 39 #include <netinet/if_ether.h> 40 41 #if NBPFILTER > 0 42 #include <net/bpf.h> 43 #endif 44 45 #include <uvm/uvm_extern.h> 46 47 #include <sparc64/dev/cbusvar.h> 48 #include <sparc64/dev/ldcvar.h> 49 #include <sparc64/dev/viovar.h> 50 51 #ifdef VNET_DEBUG 52 #define DPRINTF(x) printf x 53 #else 54 #define DPRINTF(x) 55 #endif 56 57 #define VNET_BUF_SIZE 2048 58 59 /* 128 * 64 = 8192 which is a full page */ 60 #define VNET_TX_ENTRIES 128 61 #define VNET_RX_ENTRIES 128 62 63 struct vnet_attr_info { 64 struct vio_msg_tag tag; 65 uint8_t xfer_mode; 66 uint8_t addr_type; 67 uint16_t ack_freq; 68 uint32_t _reserved1; 69 uint64_t addr; 70 uint64_t mtu; 71 uint64_t _reserved2[3]; 72 }; 73 74 /* Address types. */ 75 #define VNET_ADDR_ETHERMAC 0x01 76 77 /* Sub-Type envelopes. */ 78 #define VNET_MCAST_INFO 0x0101 79 80 #define VNET_NUM_MCAST 7 81 82 struct vnet_mcast_info { 83 struct vio_msg_tag tag; 84 uint8_t set; 85 uint8_t count; 86 uint8_t mcast_addr[VNET_NUM_MCAST][ETHER_ADDR_LEN]; 87 uint32_t _reserved; 88 }; 89 90 struct vnet_desc { 91 struct vio_dring_hdr hdr; 92 uint32_t nbytes; 93 uint32_t ncookies; 94 struct ldc_cookie cookie[2]; 95 }; 96 97 struct vnet_desc_msg { 98 struct vio_msg_tag tag; 99 uint64_t seq_no; 100 uint64_t desc_handle; 101 uint32_t nbytes; 102 uint32_t ncookies; 103 struct ldc_cookie cookie[1]; 104 }; 105 106 struct vnet_dring { 107 bus_dmamap_t vd_map; 108 bus_dma_segment_t vd_seg; 109 struct vnet_desc *vd_desc; 110 int vd_nentries; 111 }; 112 113 struct vnet_dring *vnet_dring_alloc(bus_dma_tag_t, int); 114 void vnet_dring_free(bus_dma_tag_t, struct vnet_dring *); 115 116 /* 117 * For now, we only support vNet 1.0. 118 */ 119 #define VNET_MAJOR 1 120 #define VNET_MINOR 0 121 122 /* 123 * The vNet protocol wants the IP header to be 64-bit aligned, so 124 * define out own variant of ETHER_ALIGN. 125 */ 126 #define VNET_ETHER_ALIGN 6 127 128 struct vnet_soft_desc { 129 int vsd_map_idx; 130 caddr_t vsd_buf; 131 }; 132 133 struct vnet_softc { 134 struct device sc_dv; 135 bus_space_tag_t sc_bustag; 136 bus_dma_tag_t sc_dmatag; 137 138 uint64_t sc_tx_ino; 139 uint64_t sc_rx_ino; 140 void *sc_tx_ih; 141 void *sc_rx_ih; 142 143 struct ldc_conn sc_lc; 144 145 uint16_t sc_vio_state; 146 #define VIO_SND_VER_INFO 0x0001 147 #define VIO_ACK_VER_INFO 0x0002 148 #define VIO_RCV_VER_INFO 0x0004 149 #define VIO_SND_ATTR_INFO 0x0008 150 #define VIO_ACK_ATTR_INFO 0x0010 151 #define VIO_RCV_ATTR_INFO 0x0020 152 #define VIO_SND_DRING_REG 0x0040 153 #define VIO_ACK_DRING_REG 0x0080 154 #define VIO_RCV_DRING_REG 0x0100 155 #define VIO_SND_RDX 0x0200 156 #define VIO_ACK_RDX 0x0400 157 #define VIO_RCV_RDX 0x0800 158 159 struct timeout sc_handshake_to; 160 161 uint8_t sc_xfer_mode; 162 163 uint32_t sc_local_sid; 164 uint64_t sc_dring_ident; 165 uint64_t sc_seq_no; 166 167 u_int sc_tx_prod; 168 u_int sc_tx_cons; 169 170 u_int sc_peer_state; 171 172 struct ldc_map *sc_lm; 173 struct vnet_dring *sc_vd; 174 struct vnet_soft_desc *sc_vsd; 175 #define VNET_NUM_SOFT_DESC 128 176 177 size_t sc_peer_desc_size; 178 struct ldc_cookie sc_peer_dring_cookie; 179 int sc_peer_dring_nentries; 180 181 struct pool sc_pool; 182 183 struct arpcom sc_ac; 184 struct ifmedia sc_media; 185 }; 186 187 int vnet_match(struct device *, void *, void *); 188 void vnet_attach(struct device *, struct device *, void *); 189 190 const struct cfattach vnet_ca = { 191 sizeof(struct vnet_softc), vnet_match, vnet_attach 192 }; 193 194 struct cfdriver vnet_cd = { 195 NULL, "vnet", DV_IFNET 196 }; 197 198 int vnet_tx_intr(void *); 199 int vnet_rx_intr(void *); 200 void vnet_handshake(void *); 201 202 void vio_rx_data(struct ldc_conn *, struct ldc_pkt *); 203 void vnet_rx_vio_ctrl(struct vnet_softc *, struct vio_msg *); 204 void vnet_rx_vio_ver_info(struct vnet_softc *, struct vio_msg_tag *); 205 void vnet_rx_vio_attr_info(struct vnet_softc *, struct vio_msg_tag *); 206 void vnet_rx_vio_dring_reg(struct vnet_softc *, struct vio_msg_tag *); 207 void vnet_rx_vio_rdx(struct vnet_softc *sc, struct vio_msg_tag *); 208 void vnet_rx_vio_data(struct vnet_softc *sc, struct vio_msg *); 209 void vnet_rx_vio_desc_data(struct vnet_softc *sc, struct vio_msg_tag *); 210 void vnet_rx_vio_dring_data(struct vnet_softc *sc, struct vio_msg_tag *); 211 212 void vnet_ldc_reset(struct ldc_conn *); 213 void vnet_ldc_start(struct ldc_conn *); 214 215 void vnet_sendmsg(struct vnet_softc *, void *, size_t); 216 void vnet_send_ver_info(struct vnet_softc *, uint16_t, uint16_t); 217 void vnet_send_attr_info(struct vnet_softc *); 218 void vnet_send_dring_reg(struct vnet_softc *); 219 void vio_send_rdx(struct vnet_softc *); 220 void vnet_send_dring_data(struct vnet_softc *, uint32_t); 221 222 void vnet_start(struct ifnet *); 223 void vnet_start_desc(struct ifnet *); 224 int vnet_ioctl(struct ifnet *, u_long, caddr_t); 225 void vnet_watchdog(struct ifnet *); 226 227 int vnet_media_change(struct ifnet *); 228 void vnet_media_status(struct ifnet *, struct ifmediareq *); 229 230 void vnet_link_state(struct vnet_softc *sc); 231 232 void vnet_setmulti(struct vnet_softc *, int); 233 234 void vnet_init(struct ifnet *); 235 void vnet_stop(struct ifnet *); 236 237 int 238 vnet_match(struct device *parent, void *match, void *aux) 239 { 240 struct cbus_attach_args *ca = aux; 241 242 if (strcmp(ca->ca_name, "network") == 0) 243 return (1); 244 245 return (0); 246 } 247 248 void 249 vnet_attach(struct device *parent, struct device *self, void *aux) 250 { 251 struct vnet_softc *sc = (struct vnet_softc *)self; 252 struct cbus_attach_args *ca = aux; 253 struct ldc_conn *lc; 254 struct ifnet *ifp; 255 256 sc->sc_bustag = ca->ca_bustag; 257 sc->sc_dmatag = ca->ca_dmatag; 258 sc->sc_tx_ino = ca->ca_tx_ino; 259 sc->sc_rx_ino = ca->ca_rx_ino; 260 261 printf(": ivec 0x%llx, 0x%llx", sc->sc_tx_ino, sc->sc_rx_ino); 262 263 /* 264 * Un-configure queues before registering interrupt handlers, 265 * such that we dont get any stale LDC packets or events. 266 */ 267 hv_ldc_tx_qconf(ca->ca_id, 0, 0); 268 hv_ldc_rx_qconf(ca->ca_id, 0, 0); 269 270 sc->sc_tx_ih = bus_intr_establish(ca->ca_bustag, sc->sc_tx_ino, 271 IPL_NET, BUS_INTR_ESTABLISH_MPSAFE, vnet_tx_intr, 272 sc, sc->sc_dv.dv_xname); 273 sc->sc_rx_ih = bus_intr_establish(ca->ca_bustag, sc->sc_rx_ino, 274 IPL_NET, BUS_INTR_ESTABLISH_MPSAFE, vnet_rx_intr, 275 sc, sc->sc_dv.dv_xname); 276 if (sc->sc_tx_ih == NULL || sc->sc_rx_ih == NULL) { 277 printf(", can't establish interrupt\n"); 278 return; 279 } 280 281 lc = &sc->sc_lc; 282 lc->lc_id = ca->ca_id; 283 lc->lc_sc = sc; 284 lc->lc_reset = vnet_ldc_reset; 285 lc->lc_start = vnet_ldc_start; 286 lc->lc_rx_data = vio_rx_data; 287 288 timeout_set(&sc->sc_handshake_to, vnet_handshake, sc); 289 sc->sc_peer_state = VIO_DP_STOPPED; 290 291 lc->lc_txq = ldc_queue_alloc(sc->sc_dmatag, VNET_TX_ENTRIES); 292 if (lc->lc_txq == NULL) { 293 printf(", can't allocate tx queue\n"); 294 return; 295 } 296 297 lc->lc_rxq = ldc_queue_alloc(sc->sc_dmatag, VNET_RX_ENTRIES); 298 if (lc->lc_rxq == NULL) { 299 printf(", can't allocate rx queue\n"); 300 goto free_txqueue; 301 } 302 303 if (OF_getprop(ca->ca_node, "local-mac-address", 304 sc->sc_ac.ac_enaddr, ETHER_ADDR_LEN) > 0) 305 printf(", address %s", ether_sprintf(sc->sc_ac.ac_enaddr)); 306 307 /* 308 * Each interface gets its own pool. 309 */ 310 pool_init(&sc->sc_pool, VNET_BUF_SIZE, 0, IPL_NET, 0, 311 sc->sc_dv.dv_xname, NULL); 312 313 ifp = &sc->sc_ac.ac_if; 314 ifp->if_softc = sc; 315 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 316 ifp->if_link_state = LINK_STATE_DOWN; 317 ifp->if_ioctl = vnet_ioctl; 318 ifp->if_start = vnet_start; 319 ifp->if_watchdog = vnet_watchdog; 320 strlcpy(ifp->if_xname, sc->sc_dv.dv_xname, IFNAMSIZ); 321 322 ifmedia_init(&sc->sc_media, 0, vnet_media_change, vnet_media_status); 323 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL); 324 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO); 325 326 if_attach(ifp); 327 ether_ifattach(ifp); 328 329 printf("\n"); 330 return; 331 332 free_txqueue: 333 ldc_queue_free(sc->sc_dmatag, lc->lc_txq); 334 } 335 336 int 337 vnet_tx_intr(void *arg) 338 { 339 struct vnet_softc *sc = arg; 340 struct ldc_conn *lc = &sc->sc_lc; 341 uint64_t tx_head, tx_tail, tx_state; 342 343 hv_ldc_tx_get_state(lc->lc_id, &tx_head, &tx_tail, &tx_state); 344 if (tx_state != lc->lc_tx_state) { 345 switch (tx_state) { 346 case LDC_CHANNEL_DOWN: 347 DPRINTF(("%s: Tx link down\n", __func__)); 348 break; 349 case LDC_CHANNEL_UP: 350 DPRINTF(("%s: Tx link up\n", __func__)); 351 break; 352 case LDC_CHANNEL_RESET: 353 DPRINTF(("%s: Tx link reset\n", __func__)); 354 break; 355 } 356 lc->lc_tx_state = tx_state; 357 } 358 359 return (1); 360 } 361 362 int 363 vnet_rx_intr(void *arg) 364 { 365 struct vnet_softc *sc = arg; 366 struct ldc_conn *lc = &sc->sc_lc; 367 uint64_t rx_head, rx_tail, rx_state; 368 struct ldc_pkt *lp; 369 int err; 370 371 err = hv_ldc_rx_get_state(lc->lc_id, &rx_head, &rx_tail, &rx_state); 372 if (err == H_EINVAL) 373 return (0); 374 if (err != H_EOK) { 375 printf("hv_ldc_rx_get_state %d\n", err); 376 return (0); 377 } 378 379 if (rx_state != lc->lc_rx_state) { 380 switch (rx_state) { 381 case LDC_CHANNEL_DOWN: 382 DPRINTF(("%s: Rx link down\n", __func__)); 383 lc->lc_tx_seqid = 0; 384 lc->lc_state = 0; 385 lc->lc_reset(lc); 386 if (rx_head == rx_tail) 387 break; 388 /* Discard and ack pending I/O. */ 389 DPRINTF(("setting rx qhead to %lld\n", rx_tail)); 390 err = hv_ldc_rx_set_qhead(lc->lc_id, rx_tail); 391 if (err == H_EOK) 392 break; 393 printf("%s: hv_ldc_rx_set_qhead %d\n", __func__, err); 394 break; 395 case LDC_CHANNEL_UP: 396 DPRINTF(("%s: Rx link up\n", __func__)); 397 timeout_add_msec(&sc->sc_handshake_to, 500); 398 break; 399 case LDC_CHANNEL_RESET: 400 DPRINTF(("%s: Rx link reset\n", __func__)); 401 lc->lc_tx_seqid = 0; 402 lc->lc_state = 0; 403 lc->lc_reset(lc); 404 timeout_add_msec(&sc->sc_handshake_to, 500); 405 if (rx_head == rx_tail) 406 break; 407 /* Discard and ack pending I/O. */ 408 DPRINTF(("setting rx qhead to %lld\n", rx_tail)); 409 err = hv_ldc_rx_set_qhead(lc->lc_id, rx_tail); 410 if (err == H_EOK) 411 break; 412 printf("%s: hv_ldc_rx_set_qhead %d\n", __func__, err); 413 break; 414 } 415 lc->lc_rx_state = rx_state; 416 return (1); 417 } 418 419 if (rx_head == rx_tail) 420 return (0); 421 422 lp = (struct ldc_pkt *)(lc->lc_rxq->lq_va + rx_head); 423 switch (lp->type) { 424 case LDC_CTRL: 425 ldc_rx_ctrl(lc, lp); 426 break; 427 428 case LDC_DATA: 429 ldc_rx_data(lc, lp); 430 break; 431 432 default: 433 DPRINTF(("%0x02/%0x02/%0x02\n", lp->type, lp->stype, 434 lp->ctrl)); 435 ldc_reset(lc); 436 break; 437 } 438 439 if (lc->lc_state == 0) 440 return (1); 441 442 rx_head += sizeof(*lp); 443 rx_head &= ((lc->lc_rxq->lq_nentries * sizeof(*lp)) - 1); 444 err = hv_ldc_rx_set_qhead(lc->lc_id, rx_head); 445 if (err != H_EOK) 446 printf("%s: hv_ldc_rx_set_qhead %d\n", __func__, err); 447 448 return (1); 449 } 450 451 void 452 vnet_handshake(void *arg) 453 { 454 struct vnet_softc *sc = arg; 455 456 ldc_send_vers(&sc->sc_lc); 457 } 458 459 void 460 vio_rx_data(struct ldc_conn *lc, struct ldc_pkt *lp) 461 { 462 struct vio_msg *vm = (struct vio_msg *)lp; 463 464 switch (vm->type) { 465 case VIO_TYPE_CTRL: 466 if ((lp->env & LDC_FRAG_START) == 0 && 467 (lp->env & LDC_FRAG_STOP) == 0) 468 return; 469 vnet_rx_vio_ctrl(lc->lc_sc, vm); 470 break; 471 472 case VIO_TYPE_DATA: 473 if((lp->env & LDC_FRAG_START) == 0) 474 return; 475 vnet_rx_vio_data(lc->lc_sc, vm); 476 break; 477 478 default: 479 DPRINTF(("Unhandled packet type 0x%02x\n", vm->type)); 480 ldc_reset(lc); 481 break; 482 } 483 } 484 485 void 486 vnet_rx_vio_ctrl(struct vnet_softc *sc, struct vio_msg *vm) 487 { 488 struct vio_msg_tag *tag = (struct vio_msg_tag *)&vm->type; 489 490 switch (tag->stype_env) { 491 case VIO_VER_INFO: 492 vnet_rx_vio_ver_info(sc, tag); 493 break; 494 case VIO_ATTR_INFO: 495 vnet_rx_vio_attr_info(sc, tag); 496 break; 497 case VIO_DRING_REG: 498 vnet_rx_vio_dring_reg(sc, tag); 499 break; 500 case VIO_RDX: 501 vnet_rx_vio_rdx(sc, tag); 502 break; 503 default: 504 DPRINTF(("CTRL/0x%02x/0x%04x\n", tag->stype, tag->stype_env)); 505 break; 506 } 507 } 508 509 void 510 vnet_rx_vio_ver_info(struct vnet_softc *sc, struct vio_msg_tag *tag) 511 { 512 struct vio_ver_info *vi = (struct vio_ver_info *)tag; 513 514 switch (vi->tag.stype) { 515 case VIO_SUBTYPE_INFO: 516 DPRINTF(("CTRL/INFO/VER_INFO\n")); 517 518 /* Make sure we're talking to a virtual network device. */ 519 if (vi->dev_class != VDEV_NETWORK && 520 vi->dev_class != VDEV_NETWORK_SWITCH) { 521 /* Huh, we're not talking to a network device? */ 522 printf("Not a network device\n"); 523 vi->tag.stype = VIO_SUBTYPE_NACK; 524 vnet_sendmsg(sc, vi, sizeof(*vi)); 525 return; 526 } 527 528 if (vi->major != VNET_MAJOR) { 529 vi->tag.stype = VIO_SUBTYPE_NACK; 530 vi->major = VNET_MAJOR; 531 vi->minor = VNET_MINOR; 532 vnet_sendmsg(sc, vi, sizeof(*vi)); 533 return; 534 } 535 536 vi->tag.stype = VIO_SUBTYPE_ACK; 537 vi->tag.sid = sc->sc_local_sid; 538 vi->minor = VNET_MINOR; 539 vnet_sendmsg(sc, vi, sizeof(*vi)); 540 sc->sc_vio_state |= VIO_RCV_VER_INFO; 541 break; 542 543 case VIO_SUBTYPE_ACK: 544 DPRINTF(("CTRL/ACK/VER_INFO\n")); 545 if (!ISSET(sc->sc_vio_state, VIO_SND_VER_INFO)) { 546 ldc_reset(&sc->sc_lc); 547 break; 548 } 549 sc->sc_vio_state |= VIO_ACK_VER_INFO; 550 break; 551 552 default: 553 DPRINTF(("CTRL/0x%02x/VER_INFO\n", vi->tag.stype)); 554 break; 555 } 556 557 if (ISSET(sc->sc_vio_state, VIO_RCV_VER_INFO) && 558 ISSET(sc->sc_vio_state, VIO_ACK_VER_INFO)) 559 vnet_send_attr_info(sc); 560 } 561 562 void 563 vnet_rx_vio_attr_info(struct vnet_softc *sc, struct vio_msg_tag *tag) 564 { 565 struct vnet_attr_info *ai = (struct vnet_attr_info *)tag; 566 567 switch (ai->tag.stype) { 568 case VIO_SUBTYPE_INFO: 569 DPRINTF(("CTRL/INFO/ATTR_INFO\n")); 570 sc->sc_xfer_mode = ai->xfer_mode; 571 572 ai->tag.stype = VIO_SUBTYPE_ACK; 573 ai->tag.sid = sc->sc_local_sid; 574 vnet_sendmsg(sc, ai, sizeof(*ai)); 575 sc->sc_vio_state |= VIO_RCV_ATTR_INFO; 576 break; 577 578 case VIO_SUBTYPE_ACK: 579 DPRINTF(("CTRL/ACK/ATTR_INFO\n")); 580 if (!ISSET(sc->sc_vio_state, VIO_SND_ATTR_INFO)) { 581 ldc_reset(&sc->sc_lc); 582 break; 583 } 584 sc->sc_vio_state |= VIO_ACK_ATTR_INFO; 585 break; 586 587 default: 588 DPRINTF(("CTRL/0x%02x/ATTR_INFO\n", ai->tag.stype)); 589 break; 590 } 591 592 if (ISSET(sc->sc_vio_state, VIO_RCV_ATTR_INFO) && 593 ISSET(sc->sc_vio_state, VIO_ACK_ATTR_INFO)) { 594 if (sc->sc_xfer_mode == VIO_DRING_MODE) 595 vnet_send_dring_reg(sc); 596 else 597 vio_send_rdx(sc); 598 } 599 } 600 601 void 602 vnet_rx_vio_dring_reg(struct vnet_softc *sc, struct vio_msg_tag *tag) 603 { 604 struct vio_dring_reg *dr = (struct vio_dring_reg *)tag; 605 606 switch (dr->tag.stype) { 607 case VIO_SUBTYPE_INFO: 608 DPRINTF(("CTRL/INFO/DRING_REG\n")); 609 610 sc->sc_peer_dring_nentries = dr->num_descriptors; 611 sc->sc_peer_desc_size = dr->descriptor_size; 612 sc->sc_peer_dring_cookie = dr->cookie[0]; 613 614 dr->tag.stype = VIO_SUBTYPE_ACK; 615 dr->tag.sid = sc->sc_local_sid; 616 vnet_sendmsg(sc, dr, sizeof(*dr)); 617 sc->sc_vio_state |= VIO_RCV_DRING_REG; 618 break; 619 620 case VIO_SUBTYPE_ACK: 621 DPRINTF(("CTRL/ACK/DRING_REG\n")); 622 if (!ISSET(sc->sc_vio_state, VIO_SND_DRING_REG)) { 623 ldc_reset(&sc->sc_lc); 624 break; 625 } 626 627 sc->sc_dring_ident = dr->dring_ident; 628 sc->sc_seq_no = 1; 629 630 sc->sc_vio_state |= VIO_ACK_DRING_REG; 631 break; 632 633 default: 634 DPRINTF(("CTRL/0x%02x/DRING_REG\n", dr->tag.stype)); 635 break; 636 } 637 638 if (ISSET(sc->sc_vio_state, VIO_RCV_DRING_REG) && 639 ISSET(sc->sc_vio_state, VIO_ACK_DRING_REG)) 640 vio_send_rdx(sc); 641 } 642 643 void 644 vnet_rx_vio_rdx(struct vnet_softc *sc, struct vio_msg_tag *tag) 645 { 646 struct ifnet *ifp = &sc->sc_ac.ac_if; 647 648 switch(tag->stype) { 649 case VIO_SUBTYPE_INFO: 650 DPRINTF(("CTRL/INFO/RDX\n")); 651 652 tag->stype = VIO_SUBTYPE_ACK; 653 tag->sid = sc->sc_local_sid; 654 vnet_sendmsg(sc, tag, sizeof(*tag)); 655 sc->sc_vio_state |= VIO_RCV_RDX; 656 break; 657 658 case VIO_SUBTYPE_ACK: 659 DPRINTF(("CTRL/ACK/RDX\n")); 660 if (!ISSET(sc->sc_vio_state, VIO_SND_RDX)) { 661 ldc_reset(&sc->sc_lc); 662 break; 663 } 664 sc->sc_vio_state |= VIO_ACK_RDX; 665 break; 666 667 default: 668 DPRINTF(("CTRL/0x%02x/RDX (VIO)\n", tag->stype)); 669 break; 670 } 671 672 if (ISSET(sc->sc_vio_state, VIO_RCV_RDX) && 673 ISSET(sc->sc_vio_state, VIO_ACK_RDX)) { 674 /* Link is up! */ 675 vnet_link_state(sc); 676 677 /* Configure multicast now that we can. */ 678 vnet_setmulti(sc, 1); 679 680 KERNEL_LOCK(); 681 ifq_clr_oactive(&ifp->if_snd); 682 vnet_start(ifp); 683 KERNEL_UNLOCK(); 684 } 685 } 686 687 void 688 vnet_rx_vio_data(struct vnet_softc *sc, struct vio_msg *vm) 689 { 690 struct vio_msg_tag *tag = (struct vio_msg_tag *)&vm->type; 691 692 if (!ISSET(sc->sc_vio_state, VIO_RCV_RDX) || 693 !ISSET(sc->sc_vio_state, VIO_ACK_RDX)) { 694 DPRINTF(("Spurious DATA/0x%02x/0x%04x\n", tag->stype, 695 tag->stype_env)); 696 return; 697 } 698 699 switch(tag->stype_env) { 700 case VIO_DESC_DATA: 701 vnet_rx_vio_desc_data(sc, tag); 702 break; 703 704 case VIO_DRING_DATA: 705 vnet_rx_vio_dring_data(sc, tag); 706 break; 707 708 default: 709 DPRINTF(("DATA/0x%02x/0x%04x\n", tag->stype, tag->stype_env)); 710 break; 711 } 712 } 713 714 void 715 vnet_rx_vio_desc_data(struct vnet_softc *sc, struct vio_msg_tag *tag) 716 { 717 struct vnet_desc_msg *dm = (struct vnet_desc_msg *)tag; 718 struct ldc_conn *lc = &sc->sc_lc; 719 struct ldc_map *map = sc->sc_lm; 720 struct ifnet *ifp = &sc->sc_ac.ac_if; 721 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 722 struct mbuf *m; 723 caddr_t buf; 724 paddr_t pa; 725 psize_t nbytes; 726 u_int cons; 727 int err; 728 729 switch(tag->stype) { 730 case VIO_SUBTYPE_INFO: 731 nbytes = roundup(dm->nbytes, 8); 732 if (nbytes > VNET_BUF_SIZE) { 733 ifp->if_ierrors++; 734 goto skip; 735 } 736 737 buf = pool_get(&sc->sc_pool, PR_NOWAIT|PR_ZERO); 738 if (buf == NULL) { 739 ifp->if_ierrors++; 740 goto skip; 741 } 742 743 pmap_extract(pmap_kernel(), (vaddr_t)buf, &pa); 744 err = hv_ldc_copy(lc->lc_id, LDC_COPY_IN, 745 dm->cookie[0].addr, pa, nbytes, &nbytes); 746 if (err != H_EOK) { 747 pool_put(&sc->sc_pool, buf); 748 ifp->if_ierrors++; 749 goto skip; 750 } 751 752 /* Stupid OBP doesn't align properly. */ 753 m = m_devget(buf, dm->nbytes, ETHER_ALIGN); 754 pool_put(&sc->sc_pool, buf); 755 if (m == NULL) { 756 ifp->if_ierrors++; 757 goto skip; 758 } 759 760 /* Pass it on. */ 761 ml_enqueue(&ml, m); 762 if_input(ifp, &ml); 763 764 skip: 765 dm->tag.stype = VIO_SUBTYPE_ACK; 766 dm->tag.sid = sc->sc_local_sid; 767 vnet_sendmsg(sc, dm, sizeof(*dm)); 768 break; 769 770 case VIO_SUBTYPE_ACK: 771 DPRINTF(("DATA/ACK/DESC_DATA\n")); 772 773 if (dm->desc_handle != sc->sc_tx_cons) { 774 printf("%s: out of order\n", __func__); 775 return; 776 } 777 778 cons = sc->sc_tx_cons & (sc->sc_vd->vd_nentries - 1); 779 780 map->lm_slot[sc->sc_vsd[cons].vsd_map_idx].entry = 0; 781 atomic_dec_int(&map->lm_count); 782 783 pool_put(&sc->sc_pool, sc->sc_vsd[cons].vsd_buf); 784 sc->sc_vsd[cons].vsd_buf = NULL; 785 786 sc->sc_tx_cons++; 787 break; 788 789 case VIO_SUBTYPE_NACK: 790 DPRINTF(("DATA/NACK/DESC_DATA\n")); 791 break; 792 793 default: 794 DPRINTF(("DATA/0x%02x/DESC_DATA\n", tag->stype)); 795 break; 796 } 797 } 798 799 void 800 vnet_rx_vio_dring_data(struct vnet_softc *sc, struct vio_msg_tag *tag) 801 { 802 struct vio_dring_msg *dm = (struct vio_dring_msg *)tag; 803 struct ldc_conn *lc = &sc->sc_lc; 804 struct ifnet *ifp = &sc->sc_ac.ac_if; 805 struct mbuf *m; 806 paddr_t pa; 807 psize_t nbytes; 808 int err; 809 810 switch(tag->stype) { 811 case VIO_SUBTYPE_INFO: 812 { 813 struct vnet_desc desc; 814 uint64_t cookie; 815 paddr_t desc_pa; 816 int idx, ack_end_idx = -1; 817 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 818 819 idx = dm->start_idx; 820 for (;;) { 821 cookie = sc->sc_peer_dring_cookie.addr; 822 cookie += idx * sc->sc_peer_desc_size; 823 nbytes = sc->sc_peer_desc_size; 824 pmap_extract(pmap_kernel(), (vaddr_t)&desc, &desc_pa); 825 err = hv_ldc_copy(lc->lc_id, LDC_COPY_IN, cookie, 826 desc_pa, nbytes, &nbytes); 827 if (err != H_EOK) { 828 printf("hv_ldc_copy in %d\n", err); 829 break; 830 } 831 832 if (desc.hdr.dstate != VIO_DESC_READY) 833 break; 834 835 if (desc.nbytes > (ETHER_MAX_LEN - ETHER_CRC_LEN)) { 836 ifp->if_ierrors++; 837 goto skip; 838 } 839 840 m = MCLGETL(NULL, M_DONTWAIT, desc.nbytes); 841 if (!m) 842 break; 843 m->m_len = m->m_pkthdr.len = desc.nbytes; 844 nbytes = roundup(desc.nbytes + VNET_ETHER_ALIGN, 8); 845 846 pmap_extract(pmap_kernel(), (vaddr_t)m->m_data, &pa); 847 err = hv_ldc_copy(lc->lc_id, LDC_COPY_IN, 848 desc.cookie[0].addr, pa, nbytes, &nbytes); 849 if (err != H_EOK) { 850 m_freem(m); 851 goto skip; 852 } 853 m->m_data += VNET_ETHER_ALIGN; 854 855 ml_enqueue(&ml, m); 856 857 skip: 858 desc.hdr.dstate = VIO_DESC_DONE; 859 nbytes = sc->sc_peer_desc_size; 860 err = hv_ldc_copy(lc->lc_id, LDC_COPY_OUT, cookie, 861 desc_pa, nbytes, &nbytes); 862 if (err != H_EOK) 863 printf("hv_ldc_copy out %d\n", err); 864 865 ack_end_idx = idx; 866 if (++idx == sc->sc_peer_dring_nentries) 867 idx = 0; 868 } 869 870 if_input(ifp, &ml); 871 872 if (ack_end_idx == -1) { 873 dm->tag.stype = VIO_SUBTYPE_NACK; 874 } else { 875 dm->tag.stype = VIO_SUBTYPE_ACK; 876 dm->end_idx = ack_end_idx; 877 } 878 dm->tag.sid = sc->sc_local_sid; 879 dm->proc_state = VIO_DP_STOPPED; 880 vnet_sendmsg(sc, dm, sizeof(*dm)); 881 break; 882 } 883 884 case VIO_SUBTYPE_ACK: 885 { 886 struct ldc_map *map = sc->sc_lm; 887 u_int cons, count; 888 889 sc->sc_peer_state = dm->proc_state; 890 891 cons = sc->sc_tx_cons & (sc->sc_vd->vd_nentries - 1); 892 while (sc->sc_vd->vd_desc[cons].hdr.dstate == VIO_DESC_DONE) { 893 map->lm_slot[sc->sc_vsd[cons].vsd_map_idx].entry = 0; 894 atomic_dec_int(&map->lm_count); 895 896 pool_put(&sc->sc_pool, sc->sc_vsd[cons].vsd_buf); 897 sc->sc_vsd[cons].vsd_buf = NULL; 898 899 sc->sc_vd->vd_desc[cons].hdr.dstate = VIO_DESC_FREE; 900 sc->sc_tx_cons++; 901 cons = sc->sc_tx_cons & (sc->sc_vd->vd_nentries - 1); 902 } 903 904 KERNEL_LOCK(); 905 count = sc->sc_tx_prod - sc->sc_tx_cons; 906 if (count > 0 && sc->sc_peer_state != VIO_DP_ACTIVE) 907 vnet_send_dring_data(sc, cons); 908 909 if (count < (sc->sc_vd->vd_nentries - 1)) 910 ifq_clr_oactive(&ifp->if_snd); 911 if (count == 0) 912 ifp->if_timer = 0; 913 914 vnet_start(ifp); 915 KERNEL_UNLOCK(); 916 break; 917 } 918 919 case VIO_SUBTYPE_NACK: 920 DPRINTF(("DATA/NACK/DRING_DATA\n")); 921 sc->sc_peer_state = VIO_DP_STOPPED; 922 break; 923 924 default: 925 DPRINTF(("DATA/0x%02x/DRING_DATA\n", tag->stype)); 926 break; 927 } 928 } 929 930 void 931 vnet_ldc_reset(struct ldc_conn *lc) 932 { 933 struct vnet_softc *sc = lc->lc_sc; 934 int i; 935 936 timeout_del(&sc->sc_handshake_to); 937 sc->sc_tx_prod = sc->sc_tx_cons = 0; 938 sc->sc_peer_state = VIO_DP_STOPPED; 939 sc->sc_vio_state = 0; 940 vnet_link_state(sc); 941 942 sc->sc_lm->lm_next = 1; 943 sc->sc_lm->lm_count = 1; 944 for (i = 1; i < sc->sc_lm->lm_nentries; i++) 945 sc->sc_lm->lm_slot[i].entry = 0; 946 947 for (i = 0; i < sc->sc_vd->vd_nentries; i++) { 948 if (sc->sc_vsd[i].vsd_buf) { 949 pool_put(&sc->sc_pool, sc->sc_vsd[i].vsd_buf); 950 sc->sc_vsd[i].vsd_buf = NULL; 951 } 952 sc->sc_vd->vd_desc[i].hdr.dstate = VIO_DESC_FREE; 953 } 954 } 955 956 void 957 vnet_ldc_start(struct ldc_conn *lc) 958 { 959 struct vnet_softc *sc = lc->lc_sc; 960 961 timeout_del(&sc->sc_handshake_to); 962 vnet_send_ver_info(sc, VNET_MAJOR, VNET_MINOR); 963 } 964 965 void 966 vnet_sendmsg(struct vnet_softc *sc, void *msg, size_t len) 967 { 968 struct ldc_conn *lc = &sc->sc_lc; 969 int err; 970 971 err = ldc_send_unreliable(lc, msg, len); 972 if (err) 973 printf("%s: ldc_send_unreliable: %d\n", __func__, err); 974 } 975 976 void 977 vnet_send_ver_info(struct vnet_softc *sc, uint16_t major, uint16_t minor) 978 { 979 struct vio_ver_info vi; 980 981 bzero(&vi, sizeof(vi)); 982 vi.tag.type = VIO_TYPE_CTRL; 983 vi.tag.stype = VIO_SUBTYPE_INFO; 984 vi.tag.stype_env = VIO_VER_INFO; 985 vi.tag.sid = sc->sc_local_sid; 986 vi.major = major; 987 vi.minor = minor; 988 vi.dev_class = VDEV_NETWORK; 989 vnet_sendmsg(sc, &vi, sizeof(vi)); 990 991 sc->sc_vio_state |= VIO_SND_VER_INFO; 992 } 993 994 void 995 vnet_send_attr_info(struct vnet_softc *sc) 996 { 997 struct vnet_attr_info ai; 998 int i; 999 1000 bzero(&ai, sizeof(ai)); 1001 ai.tag.type = VIO_TYPE_CTRL; 1002 ai.tag.stype = VIO_SUBTYPE_INFO; 1003 ai.tag.stype_env = VIO_ATTR_INFO; 1004 ai.tag.sid = sc->sc_local_sid; 1005 ai.xfer_mode = VIO_DRING_MODE; 1006 ai.addr_type = VNET_ADDR_ETHERMAC; 1007 ai.ack_freq = 0; 1008 ai.addr = 0; 1009 for (i = 0; i < ETHER_ADDR_LEN; i++) { 1010 ai.addr <<= 8; 1011 ai.addr |= sc->sc_ac.ac_enaddr[i]; 1012 } 1013 ai.mtu = ETHER_MAX_LEN - ETHER_CRC_LEN; 1014 vnet_sendmsg(sc, &ai, sizeof(ai)); 1015 1016 sc->sc_vio_state |= VIO_SND_ATTR_INFO; 1017 } 1018 1019 void 1020 vnet_send_dring_reg(struct vnet_softc *sc) 1021 { 1022 struct vio_dring_reg dr; 1023 1024 bzero(&dr, sizeof(dr)); 1025 dr.tag.type = VIO_TYPE_CTRL; 1026 dr.tag.stype = VIO_SUBTYPE_INFO; 1027 dr.tag.stype_env = VIO_DRING_REG; 1028 dr.tag.sid = sc->sc_local_sid; 1029 dr.dring_ident = 0; 1030 dr.num_descriptors = sc->sc_vd->vd_nentries; 1031 dr.descriptor_size = sizeof(struct vnet_desc); 1032 dr.options = VIO_TX_RING; 1033 dr.ncookies = 1; 1034 dr.cookie[0].addr = 0; 1035 dr.cookie[0].size = PAGE_SIZE; 1036 vnet_sendmsg(sc, &dr, sizeof(dr)); 1037 1038 sc->sc_vio_state |= VIO_SND_DRING_REG; 1039 }; 1040 1041 void 1042 vio_send_rdx(struct vnet_softc *sc) 1043 { 1044 struct vio_msg_tag tag; 1045 1046 tag.type = VIO_TYPE_CTRL; 1047 tag.stype = VIO_SUBTYPE_INFO; 1048 tag.stype_env = VIO_RDX; 1049 tag.sid = sc->sc_local_sid; 1050 vnet_sendmsg(sc, &tag, sizeof(tag)); 1051 1052 sc->sc_vio_state |= VIO_SND_RDX; 1053 } 1054 1055 void 1056 vnet_send_dring_data(struct vnet_softc *sc, uint32_t start_idx) 1057 { 1058 struct vio_dring_msg dm; 1059 u_int peer_state; 1060 1061 peer_state = atomic_swap_uint(&sc->sc_peer_state, VIO_DP_ACTIVE); 1062 if (peer_state == VIO_DP_ACTIVE) 1063 return; 1064 1065 bzero(&dm, sizeof(dm)); 1066 dm.tag.type = VIO_TYPE_DATA; 1067 dm.tag.stype = VIO_SUBTYPE_INFO; 1068 dm.tag.stype_env = VIO_DRING_DATA; 1069 dm.tag.sid = sc->sc_local_sid; 1070 dm.seq_no = sc->sc_seq_no++; 1071 dm.dring_ident = sc->sc_dring_ident; 1072 dm.start_idx = start_idx; 1073 dm.end_idx = -1; 1074 vnet_sendmsg(sc, &dm, sizeof(dm)); 1075 } 1076 1077 void 1078 vnet_start(struct ifnet *ifp) 1079 { 1080 struct vnet_softc *sc = ifp->if_softc; 1081 struct ldc_conn *lc = &sc->sc_lc; 1082 struct ldc_map *map = sc->sc_lm; 1083 struct mbuf *m; 1084 paddr_t pa; 1085 caddr_t buf; 1086 uint64_t tx_head, tx_tail, tx_state; 1087 u_int start, prod, count; 1088 int err; 1089 1090 if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd)) 1091 return; 1092 1093 if (ifq_empty(&ifp->if_snd)) 1094 return; 1095 1096 /* 1097 * We cannot transmit packets until a VIO connection has been 1098 * established. 1099 */ 1100 if (!ISSET(sc->sc_vio_state, VIO_RCV_RDX) || 1101 !ISSET(sc->sc_vio_state, VIO_ACK_RDX)) 1102 return; 1103 1104 /* 1105 * Make sure there is room in the LDC transmit queue to send a 1106 * DRING_DATA message. 1107 */ 1108 err = hv_ldc_tx_get_state(lc->lc_id, &tx_head, &tx_tail, &tx_state); 1109 if (err != H_EOK) 1110 return; 1111 tx_tail += sizeof(struct ldc_pkt); 1112 tx_tail &= ((lc->lc_txq->lq_nentries * sizeof(struct ldc_pkt)) - 1); 1113 if (tx_tail == tx_head) { 1114 ifq_set_oactive(&ifp->if_snd); 1115 return; 1116 } 1117 1118 if (sc->sc_xfer_mode == VIO_DESC_MODE) { 1119 vnet_start_desc(ifp); 1120 return; 1121 } 1122 1123 start = prod = sc->sc_tx_prod & (sc->sc_vd->vd_nentries - 1); 1124 while (sc->sc_vd->vd_desc[prod].hdr.dstate == VIO_DESC_FREE) { 1125 count = sc->sc_tx_prod - sc->sc_tx_cons; 1126 if (count >= (sc->sc_vd->vd_nentries - 1) || 1127 map->lm_count >= map->lm_nentries) { 1128 ifq_set_oactive(&ifp->if_snd); 1129 break; 1130 } 1131 1132 buf = pool_get(&sc->sc_pool, PR_NOWAIT|PR_ZERO); 1133 if (buf == NULL) { 1134 ifq_set_oactive(&ifp->if_snd); 1135 break; 1136 } 1137 1138 m = ifq_dequeue(&ifp->if_snd); 1139 if (m == NULL) { 1140 pool_put(&sc->sc_pool, buf); 1141 break; 1142 } 1143 1144 if (m->m_pkthdr.len > VNET_BUF_SIZE - VNET_ETHER_ALIGN) { 1145 ifp->if_oerrors++; 1146 pool_put(&sc->sc_pool, buf); 1147 m_freem(m); 1148 break; 1149 } 1150 m_copydata(m, 0, m->m_pkthdr.len, buf + VNET_ETHER_ALIGN); 1151 1152 #if NBPFILTER > 0 1153 /* 1154 * If BPF is listening on this interface, let it see the 1155 * packet before we commit it to the wire. 1156 */ 1157 if (ifp->if_bpf) 1158 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 1159 #endif 1160 1161 pmap_extract(pmap_kernel(), (vaddr_t)buf, &pa); 1162 KASSERT((pa & ~PAGE_MASK) == (pa & LDC_MTE_RA_MASK)); 1163 while (map->lm_slot[map->lm_next].entry != 0) { 1164 map->lm_next++; 1165 map->lm_next &= (map->lm_nentries - 1); 1166 } 1167 map->lm_slot[map->lm_next].entry = (pa & LDC_MTE_RA_MASK); 1168 map->lm_slot[map->lm_next].entry |= LDC_MTE_CPR; 1169 atomic_inc_int(&map->lm_count); 1170 1171 sc->sc_vd->vd_desc[prod].nbytes = max(m->m_pkthdr.len, 60); 1172 sc->sc_vd->vd_desc[prod].ncookies = 1; 1173 sc->sc_vd->vd_desc[prod].cookie[0].addr = 1174 map->lm_next << PAGE_SHIFT | (pa & PAGE_MASK); 1175 sc->sc_vd->vd_desc[prod].cookie[0].size = VNET_BUF_SIZE; 1176 if (prod != start) 1177 sc->sc_vd->vd_desc[prod].hdr.dstate = VIO_DESC_READY; 1178 1179 sc->sc_vsd[prod].vsd_map_idx = map->lm_next; 1180 sc->sc_vsd[prod].vsd_buf = buf; 1181 1182 sc->sc_tx_prod++; 1183 prod = sc->sc_tx_prod & (sc->sc_vd->vd_nentries - 1); 1184 1185 m_freem(m); 1186 } 1187 1188 1189 if (start != prod) { 1190 membar_producer(); 1191 sc->sc_vd->vd_desc[start].hdr.dstate = VIO_DESC_READY; 1192 if (sc->sc_peer_state != VIO_DP_ACTIVE) { 1193 vnet_send_dring_data(sc, start); 1194 ifp->if_timer = 10; 1195 } 1196 } 1197 } 1198 1199 void 1200 vnet_start_desc(struct ifnet *ifp) 1201 { 1202 struct vnet_softc *sc = ifp->if_softc; 1203 struct ldc_map *map = sc->sc_lm; 1204 struct vnet_desc_msg dm; 1205 struct mbuf *m; 1206 paddr_t pa; 1207 caddr_t buf; 1208 u_int prod, count; 1209 1210 for (;;) { 1211 count = sc->sc_tx_prod - sc->sc_tx_cons; 1212 if (count >= (sc->sc_vd->vd_nentries - 1) || 1213 map->lm_count >= map->lm_nentries) { 1214 ifq_set_oactive(&ifp->if_snd); 1215 return; 1216 } 1217 1218 buf = pool_get(&sc->sc_pool, PR_NOWAIT|PR_ZERO); 1219 if (buf == NULL) { 1220 ifq_set_oactive(&ifp->if_snd); 1221 return; 1222 } 1223 1224 m = ifq_dequeue(&ifp->if_snd); 1225 if (m == NULL) { 1226 pool_put(&sc->sc_pool, buf); 1227 return; 1228 } 1229 1230 m_copydata(m, 0, m->m_pkthdr.len, buf); 1231 1232 #if NBPFILTER > 0 1233 /* 1234 * If BPF is listening on this interface, let it see the 1235 * packet before we commit it to the wire. 1236 */ 1237 if (ifp->if_bpf) 1238 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 1239 #endif 1240 1241 pmap_extract(pmap_kernel(), (vaddr_t)buf, &pa); 1242 KASSERT((pa & ~PAGE_MASK) == (pa & LDC_MTE_RA_MASK)); 1243 while (map->lm_slot[map->lm_next].entry != 0) { 1244 map->lm_next++; 1245 map->lm_next &= (map->lm_nentries - 1); 1246 } 1247 map->lm_slot[map->lm_next].entry = (pa & LDC_MTE_RA_MASK); 1248 map->lm_slot[map->lm_next].entry |= LDC_MTE_CPR; 1249 atomic_inc_int(&map->lm_count); 1250 1251 prod = sc->sc_tx_prod & (sc->sc_vd->vd_nentries - 1); 1252 sc->sc_vsd[prod].vsd_map_idx = map->lm_next; 1253 sc->sc_vsd[prod].vsd_buf = buf; 1254 1255 bzero(&dm, sizeof(dm)); 1256 dm.tag.type = VIO_TYPE_DATA; 1257 dm.tag.stype = VIO_SUBTYPE_INFO; 1258 dm.tag.stype_env = VIO_DESC_DATA; 1259 dm.tag.sid = sc->sc_local_sid; 1260 dm.seq_no = sc->sc_seq_no++; 1261 dm.desc_handle = sc->sc_tx_prod; 1262 dm.nbytes = max(m->m_pkthdr.len, 60); 1263 dm.ncookies = 1; 1264 dm.cookie[0].addr = 1265 map->lm_next << PAGE_SHIFT | (pa & PAGE_MASK); 1266 dm.cookie[0].size = VNET_BUF_SIZE; 1267 vnet_sendmsg(sc, &dm, sizeof(dm)); 1268 1269 sc->sc_tx_prod++; 1270 sc->sc_tx_prod &= (sc->sc_vd->vd_nentries - 1); 1271 1272 m_freem(m); 1273 } 1274 } 1275 1276 int 1277 vnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1278 { 1279 struct vnet_softc *sc = ifp->if_softc; 1280 struct ifreq *ifr = (struct ifreq *)data; 1281 int s, error = 0; 1282 1283 s = splnet(); 1284 1285 switch (cmd) { 1286 case SIOCSIFADDR: 1287 ifp->if_flags |= IFF_UP; 1288 /* FALLTHROUGH */ 1289 case SIOCSIFFLAGS: 1290 if (ifp->if_flags & IFF_UP) { 1291 if ((ifp->if_flags & IFF_RUNNING) == 0) 1292 vnet_init(ifp); 1293 } else { 1294 if (ifp->if_flags & IFF_RUNNING) 1295 vnet_stop(ifp); 1296 } 1297 break; 1298 1299 case SIOCGIFMEDIA: 1300 case SIOCSIFMEDIA: 1301 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 1302 break; 1303 1304 case SIOCADDMULTI: 1305 case SIOCDELMULTI: 1306 /* 1307 * XXX Removing all multicast addresses and adding 1308 * most of them back, is somewhat retarded. 1309 */ 1310 vnet_setmulti(sc, 0); 1311 error = ether_ioctl(ifp, &sc->sc_ac, cmd, data); 1312 vnet_setmulti(sc, 1); 1313 if (error == ENETRESET) 1314 error = 0; 1315 break; 1316 1317 default: 1318 error = ether_ioctl(ifp, &sc->sc_ac, cmd, data); 1319 } 1320 1321 splx(s); 1322 return (error); 1323 } 1324 1325 void 1326 vnet_watchdog(struct ifnet *ifp) 1327 { 1328 struct vnet_softc *sc = ifp->if_softc; 1329 1330 printf("%s: watchdog timeout\n", sc->sc_dv.dv_xname); 1331 } 1332 1333 int 1334 vnet_media_change(struct ifnet *ifp) 1335 { 1336 return (0); 1337 } 1338 1339 void 1340 vnet_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1341 { 1342 imr->ifm_active = IFM_ETHER | IFM_AUTO; 1343 imr->ifm_status = IFM_AVALID; 1344 1345 if (LINK_STATE_IS_UP(ifp->if_link_state) && 1346 ifp->if_flags & IFF_UP) 1347 imr->ifm_status |= IFM_ACTIVE; 1348 } 1349 1350 void 1351 vnet_link_state(struct vnet_softc *sc) 1352 { 1353 struct ifnet *ifp = &sc->sc_ac.ac_if; 1354 int link_state = LINK_STATE_DOWN; 1355 1356 KERNEL_LOCK(); 1357 if (ISSET(sc->sc_vio_state, VIO_RCV_RDX) && 1358 ISSET(sc->sc_vio_state, VIO_ACK_RDX)) 1359 link_state = LINK_STATE_FULL_DUPLEX; 1360 if (ifp->if_link_state != link_state) { 1361 ifp->if_link_state = link_state; 1362 if_link_state_change(ifp); 1363 } 1364 KERNEL_UNLOCK(); 1365 } 1366 1367 void 1368 vnet_setmulti(struct vnet_softc *sc, int set) 1369 { 1370 struct arpcom *ac = &sc->sc_ac; 1371 struct ether_multi *enm; 1372 struct ether_multistep step; 1373 struct vnet_mcast_info mi; 1374 int count = 0; 1375 1376 if (!ISSET(sc->sc_vio_state, VIO_RCV_RDX) || 1377 !ISSET(sc->sc_vio_state, VIO_ACK_RDX)) 1378 return; 1379 1380 bzero(&mi, sizeof(mi)); 1381 mi.tag.type = VIO_TYPE_CTRL; 1382 mi.tag.stype = VIO_SUBTYPE_INFO; 1383 mi.tag.stype_env = VNET_MCAST_INFO; 1384 mi.tag.sid = sc->sc_local_sid; 1385 mi.set = set ? 1 : 0; 1386 KERNEL_LOCK(); 1387 ETHER_FIRST_MULTI(step, ac, enm); 1388 while (enm != NULL) { 1389 /* XXX What about multicast ranges? */ 1390 bcopy(enm->enm_addrlo, mi.mcast_addr[count], ETHER_ADDR_LEN); 1391 ETHER_NEXT_MULTI(step, enm); 1392 1393 count++; 1394 if (count < VNET_NUM_MCAST) 1395 continue; 1396 1397 mi.count = VNET_NUM_MCAST; 1398 vnet_sendmsg(sc, &mi, sizeof(mi)); 1399 count = 0; 1400 } 1401 1402 if (count > 0) { 1403 mi.count = count; 1404 vnet_sendmsg(sc, &mi, sizeof(mi)); 1405 } 1406 KERNEL_UNLOCK(); 1407 } 1408 1409 void 1410 vnet_init(struct ifnet *ifp) 1411 { 1412 struct vnet_softc *sc = ifp->if_softc; 1413 struct ldc_conn *lc = &sc->sc_lc; 1414 int err; 1415 1416 sc->sc_lm = ldc_map_alloc(sc->sc_dmatag, 2048); 1417 if (sc->sc_lm == NULL) 1418 return; 1419 1420 err = hv_ldc_set_map_table(lc->lc_id, 1421 sc->sc_lm->lm_map->dm_segs[0].ds_addr, sc->sc_lm->lm_nentries); 1422 if (err != H_EOK) { 1423 printf("hv_ldc_set_map_table %d\n", err); 1424 return; 1425 } 1426 1427 sc->sc_vd = vnet_dring_alloc(sc->sc_dmatag, VNET_NUM_SOFT_DESC); 1428 if (sc->sc_vd == NULL) 1429 return; 1430 sc->sc_vsd = malloc(VNET_NUM_SOFT_DESC * sizeof(*sc->sc_vsd), M_DEVBUF, 1431 M_NOWAIT|M_ZERO); 1432 if (sc->sc_vsd == NULL) 1433 return; 1434 1435 sc->sc_lm->lm_slot[0].entry = sc->sc_vd->vd_map->dm_segs[0].ds_addr; 1436 sc->sc_lm->lm_slot[0].entry &= LDC_MTE_RA_MASK; 1437 sc->sc_lm->lm_slot[0].entry |= LDC_MTE_CPR | LDC_MTE_CPW; 1438 sc->sc_lm->lm_next = 1; 1439 sc->sc_lm->lm_count = 1; 1440 1441 err = hv_ldc_tx_qconf(lc->lc_id, 1442 lc->lc_txq->lq_map->dm_segs[0].ds_addr, lc->lc_txq->lq_nentries); 1443 if (err != H_EOK) 1444 printf("hv_ldc_tx_qconf %d\n", err); 1445 1446 err = hv_ldc_rx_qconf(lc->lc_id, 1447 lc->lc_rxq->lq_map->dm_segs[0].ds_addr, lc->lc_rxq->lq_nentries); 1448 if (err != H_EOK) 1449 printf("hv_ldc_rx_qconf %d\n", err); 1450 1451 cbus_intr_setenabled(sc->sc_bustag, sc->sc_tx_ino, INTR_ENABLED); 1452 cbus_intr_setenabled(sc->sc_bustag, sc->sc_rx_ino, INTR_ENABLED); 1453 1454 ldc_send_vers(lc); 1455 1456 ifp->if_flags |= IFF_RUNNING; 1457 } 1458 1459 void 1460 vnet_stop(struct ifnet *ifp) 1461 { 1462 struct vnet_softc *sc = ifp->if_softc; 1463 struct ldc_conn *lc = &sc->sc_lc; 1464 1465 ifp->if_flags &= ~IFF_RUNNING; 1466 ifq_clr_oactive(&ifp->if_snd); 1467 ifp->if_timer = 0; 1468 1469 cbus_intr_setenabled(sc->sc_bustag, sc->sc_tx_ino, INTR_DISABLED); 1470 cbus_intr_setenabled(sc->sc_bustag, sc->sc_rx_ino, INTR_DISABLED); 1471 1472 intr_barrier(sc->sc_tx_ih); 1473 intr_barrier(sc->sc_rx_ih); 1474 1475 hv_ldc_tx_qconf(lc->lc_id, 0, 0); 1476 hv_ldc_rx_qconf(lc->lc_id, 0, 0); 1477 lc->lc_tx_seqid = 0; 1478 lc->lc_state = 0; 1479 lc->lc_tx_state = lc->lc_rx_state = LDC_CHANNEL_DOWN; 1480 vnet_ldc_reset(lc); 1481 1482 free(sc->sc_vsd, M_DEVBUF, VNET_NUM_SOFT_DESC * sizeof(*sc->sc_vsd)); 1483 1484 vnet_dring_free(sc->sc_dmatag, sc->sc_vd); 1485 1486 hv_ldc_set_map_table(lc->lc_id, 0, 0); 1487 ldc_map_free(sc->sc_dmatag, sc->sc_lm); 1488 } 1489 1490 struct vnet_dring * 1491 vnet_dring_alloc(bus_dma_tag_t t, int nentries) 1492 { 1493 struct vnet_dring *vd; 1494 bus_size_t size; 1495 caddr_t va; 1496 int nsegs; 1497 int i; 1498 1499 vd = malloc(sizeof(struct vnet_dring), M_DEVBUF, M_NOWAIT); 1500 if (vd == NULL) 1501 return NULL; 1502 1503 size = roundup(nentries * sizeof(struct vnet_desc), PAGE_SIZE); 1504 1505 if (bus_dmamap_create(t, size, 1, size, 0, 1506 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &vd->vd_map) != 0) 1507 return (NULL); 1508 1509 if (bus_dmamem_alloc(t, size, PAGE_SIZE, 0, &vd->vd_seg, 1, 1510 &nsegs, BUS_DMA_NOWAIT) != 0) 1511 goto destroy; 1512 1513 if (bus_dmamem_map(t, &vd->vd_seg, 1, size, &va, 1514 BUS_DMA_NOWAIT) != 0) 1515 goto free; 1516 1517 if (bus_dmamap_load(t, vd->vd_map, va, size, NULL, 1518 BUS_DMA_NOWAIT) != 0) 1519 goto unmap; 1520 1521 vd->vd_desc = (struct vnet_desc *)va; 1522 vd->vd_nentries = nentries; 1523 bzero(vd->vd_desc, nentries * sizeof(struct vnet_desc)); 1524 for (i = 0; i < vd->vd_nentries; i++) 1525 vd->vd_desc[i].hdr.dstate = VIO_DESC_FREE; 1526 return (vd); 1527 1528 unmap: 1529 bus_dmamem_unmap(t, va, size); 1530 free: 1531 bus_dmamem_free(t, &vd->vd_seg, 1); 1532 destroy: 1533 bus_dmamap_destroy(t, vd->vd_map); 1534 1535 return (NULL); 1536 } 1537 1538 void 1539 vnet_dring_free(bus_dma_tag_t t, struct vnet_dring *vd) 1540 { 1541 bus_size_t size; 1542 1543 size = vd->vd_nentries * sizeof(struct vnet_desc); 1544 size = roundup(size, PAGE_SIZE); 1545 1546 bus_dmamap_unload(t, vd->vd_map); 1547 bus_dmamem_unmap(t, (caddr_t)vd->vd_desc, size); 1548 bus_dmamem_free(t, &vd->vd_seg, 1); 1549 bus_dmamap_destroy(t, vd->vd_map); 1550 free(vd, M_DEVBUF, sizeof(*vd)); 1551 } 1552