1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2016 iXsystems Inc. 5 * All rights reserved. 6 * 7 * This software was developed by Jakub Klama <jceel@FreeBSD.org> 8 * under sponsorship from iXsystems Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer 15 * in this position and unchanged. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright 2018 Joyent, Inc. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #ifndef WITHOUT_CAPSICUM 42 #include <sys/capsicum.h> 43 #endif 44 #include <sys/linker_set.h> 45 #include <sys/uio.h> 46 #include <sys/types.h> 47 #include <sys/socket.h> 48 #include <sys/un.h> 49 50 #ifndef WITHOUT_CAPSICUM 51 #include <capsicum_helpers.h> 52 #endif 53 #include <err.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <stdbool.h> 59 #include <string.h> 60 #include <unistd.h> 61 #include <assert.h> 62 #include <pthread.h> 63 #include <libgen.h> 64 #include <sysexits.h> 65 66 #include "bhyverun.h" 67 #include "config.h" 68 #include "debug.h" 69 #include "pci_emul.h" 70 #include "virtio.h" 71 #include "mevent.h" 72 #include "sockstream.h" 73 74 #define VTCON_RINGSZ 64 75 #define VTCON_MAXPORTS 16 76 #define VTCON_MAXQ (VTCON_MAXPORTS * 2 + 2) 77 78 #define VTCON_DEVICE_READY 0 79 #define VTCON_DEVICE_ADD 1 80 #define VTCON_DEVICE_REMOVE 2 81 #define VTCON_PORT_READY 3 82 #define VTCON_CONSOLE_PORT 4 83 #define VTCON_CONSOLE_RESIZE 5 84 #define VTCON_PORT_OPEN 6 85 #define VTCON_PORT_NAME 7 86 87 #define VTCON_F_SIZE 0 88 #define VTCON_F_MULTIPORT 1 89 #define VTCON_F_EMERG_WRITE 2 90 #define VTCON_S_HOSTCAPS \ 91 (VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE) 92 93 static int pci_vtcon_debug; 94 #define DPRINTF(params) if (pci_vtcon_debug) PRINTLN params 95 #define WPRINTF(params) PRINTLN params 96 97 struct pci_vtcon_softc; 98 struct pci_vtcon_port; 99 struct pci_vtcon_config; 100 typedef void (pci_vtcon_cb_t)(struct pci_vtcon_port *, void *, struct iovec *, 101 int); 102 103 struct pci_vtcon_port { 104 struct pci_vtcon_softc * vsp_sc; 105 int vsp_id; 106 const char * vsp_name; 107 bool vsp_enabled; 108 bool vsp_console; 109 bool vsp_rx_ready; 110 bool vsp_open; 111 int vsp_rxq; 112 int vsp_txq; 113 void * vsp_arg; 114 pci_vtcon_cb_t * vsp_cb; 115 }; 116 117 struct pci_vtcon_sock 118 { 119 struct pci_vtcon_port * vss_port; 120 const char * vss_path; 121 struct mevent * vss_server_evp; 122 struct mevent * vss_conn_evp; 123 int vss_server_fd; 124 int vss_conn_fd; 125 bool vss_open; 126 }; 127 128 struct pci_vtcon_softc { 129 struct virtio_softc vsc_vs; 130 struct vqueue_info vsc_queues[VTCON_MAXQ]; 131 pthread_mutex_t vsc_mtx; 132 uint64_t vsc_cfg; 133 uint64_t vsc_features; 134 char * vsc_rootdir; 135 int vsc_kq; 136 bool vsc_ready; 137 struct pci_vtcon_port vsc_control_port; 138 struct pci_vtcon_port vsc_ports[VTCON_MAXPORTS]; 139 struct pci_vtcon_config *vsc_config; 140 }; 141 142 struct pci_vtcon_config { 143 uint16_t cols; 144 uint16_t rows; 145 uint32_t max_nr_ports; 146 uint32_t emerg_wr; 147 } __attribute__((packed)); 148 149 struct pci_vtcon_control { 150 uint32_t id; 151 uint16_t event; 152 uint16_t value; 153 } __attribute__((packed)); 154 155 struct pci_vtcon_console_resize { 156 uint16_t cols; 157 uint16_t rows; 158 } __attribute__((packed)); 159 160 static void pci_vtcon_reset(void *); 161 static void pci_vtcon_notify_rx(void *, struct vqueue_info *); 162 static void pci_vtcon_notify_tx(void *, struct vqueue_info *); 163 static int pci_vtcon_cfgread(void *, int, int, uint32_t *); 164 static int pci_vtcon_cfgwrite(void *, int, int, uint32_t); 165 static void pci_vtcon_neg_features(void *, uint64_t); 166 static void pci_vtcon_sock_accept(int, enum ev_type, void *); 167 static void pci_vtcon_sock_rx(int, enum ev_type, void *); 168 static void pci_vtcon_sock_tx(struct pci_vtcon_port *, void *, struct iovec *, 169 int); 170 static void pci_vtcon_control_send(struct pci_vtcon_softc *, 171 struct pci_vtcon_control *, const void *, size_t); 172 static void pci_vtcon_announce_port(struct pci_vtcon_port *); 173 static void pci_vtcon_open_port(struct pci_vtcon_port *, bool); 174 175 static struct virtio_consts vtcon_vi_consts = { 176 "vtcon", /* our name */ 177 VTCON_MAXQ, /* we support VTCON_MAXQ virtqueues */ 178 sizeof(struct pci_vtcon_config), /* config reg size */ 179 pci_vtcon_reset, /* reset */ 180 NULL, /* device-wide qnotify */ 181 pci_vtcon_cfgread, /* read virtio config */ 182 pci_vtcon_cfgwrite, /* write virtio config */ 183 pci_vtcon_neg_features, /* apply negotiated features */ 184 VTCON_S_HOSTCAPS, /* our capabilities */ 185 }; 186 187 188 static void 189 pci_vtcon_reset(void *vsc) 190 { 191 struct pci_vtcon_softc *sc; 192 193 sc = vsc; 194 195 DPRINTF(("vtcon: device reset requested!")); 196 vi_reset_dev(&sc->vsc_vs); 197 } 198 199 static void 200 pci_vtcon_neg_features(void *vsc, uint64_t negotiated_features) 201 { 202 struct pci_vtcon_softc *sc = vsc; 203 204 sc->vsc_features = negotiated_features; 205 } 206 207 static int 208 pci_vtcon_cfgread(void *vsc, int offset, int size, uint32_t *retval) 209 { 210 struct pci_vtcon_softc *sc = vsc; 211 void *ptr; 212 213 ptr = (uint8_t *)sc->vsc_config + offset; 214 memcpy(retval, ptr, size); 215 return (0); 216 } 217 218 static int 219 pci_vtcon_cfgwrite(void *vsc, int offset, int size, uint32_t val) 220 { 221 222 return (0); 223 } 224 225 static inline struct pci_vtcon_port * 226 pci_vtcon_vq_to_port(struct pci_vtcon_softc *sc, struct vqueue_info *vq) 227 { 228 uint16_t num = vq->vq_num; 229 230 if (num == 0 || num == 1) 231 return (&sc->vsc_ports[0]); 232 233 if (num == 2 || num == 3) 234 return (&sc->vsc_control_port); 235 236 return (&sc->vsc_ports[(num / 2) - 1]); 237 } 238 239 static inline struct vqueue_info * 240 pci_vtcon_port_to_vq(struct pci_vtcon_port *port, bool tx_queue) 241 { 242 int qnum; 243 244 qnum = tx_queue ? port->vsp_txq : port->vsp_rxq; 245 return (&port->vsp_sc->vsc_queues[qnum]); 246 } 247 248 static struct pci_vtcon_port * 249 pci_vtcon_port_add(struct pci_vtcon_softc *sc, int port_id, const char *name, 250 pci_vtcon_cb_t *cb, void *arg) 251 { 252 struct pci_vtcon_port *port; 253 254 port = &sc->vsc_ports[port_id]; 255 if (port->vsp_enabled) { 256 errno = EBUSY; 257 return (NULL); 258 } 259 port->vsp_id = port_id; 260 port->vsp_sc = sc; 261 port->vsp_name = name; 262 port->vsp_cb = cb; 263 port->vsp_arg = arg; 264 265 if (port->vsp_id == 0) { 266 /* port0 */ 267 port->vsp_txq = 0; 268 port->vsp_rxq = 1; 269 } else { 270 port->vsp_txq = (port_id + 1) * 2; 271 port->vsp_rxq = port->vsp_txq + 1; 272 } 273 274 port->vsp_enabled = true; 275 return (port); 276 } 277 278 static int 279 pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *port_name, 280 const nvlist_t *nvl) 281 { 282 #ifdef __FreeBSD__ 283 struct pci_vtcon_sock *sock; 284 struct sockaddr_un sun; 285 #else 286 struct pci_vtcon_sock *sock = NULL; 287 /* Our compiler #defines 'sun' as '1'. Awesome. */ 288 struct sockaddr_un addr; 289 #endif 290 const char *name, *path; 291 char *cp, *pathcopy; 292 long port; 293 int s = -1, fd = -1, error = 0; 294 #ifndef WITHOUT_CAPSICUM 295 cap_rights_t rights; 296 #endif 297 298 port = strtol(port_name, &cp, 0); 299 if (*cp != '\0' || port < 0 || port >= VTCON_MAXPORTS) { 300 EPRINTLN("vtcon: Invalid port %s", port_name); 301 error = -1; 302 goto out; 303 } 304 305 path = get_config_value_node(nvl, "path"); 306 if (path == NULL) { 307 EPRINTLN("vtcon: required path missing for port %ld", port); 308 error = -1; 309 goto out; 310 } 311 312 sock = calloc(1, sizeof(struct pci_vtcon_sock)); 313 if (sock == NULL) { 314 error = -1; 315 goto out; 316 } 317 318 s = socket(AF_UNIX, SOCK_STREAM, 0); 319 if (s < 0) { 320 error = -1; 321 goto out; 322 } 323 324 #ifdef __FreeBSD__ 325 pathcopy = strdup(path); 326 if (pathcopy == NULL) { 327 error = -1; 328 goto out; 329 } 330 331 fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY); 332 if (fd < 0) { 333 free(pathcopy); 334 error = -1; 335 goto out; 336 } 337 338 sun.sun_family = AF_UNIX; 339 sun.sun_len = sizeof(struct sockaddr_un); 340 strcpy(pathcopy, path); 341 strlcpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path)); 342 free(pathcopy); 343 344 if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) { 345 error = -1; 346 goto out; 347 } 348 #else /* __FreeBSD__ */ 349 /* Do a simple bind rather than the FreeBSD bindat() */ 350 pathcopy = (char *)path; 351 addr.sun_family = AF_UNIX; 352 (void) strlcpy(addr.sun_path, pathcopy, sizeof (addr.sun_path)); 353 if (bind(fd, (struct sockaddr *)&addr, sizeof (addr)) < 0) { 354 error = -1; 355 goto out; 356 } 357 #endif /* __FreeBSD__ */ 358 359 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) { 360 error = -1; 361 goto out; 362 } 363 364 if (listen(s, 1) < 0) { 365 error = -1; 366 goto out; 367 } 368 369 #ifndef WITHOUT_CAPSICUM 370 cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE); 371 if (caph_rights_limit(s, &rights) == -1) 372 errx(EX_OSERR, "Unable to apply rights for sandbox"); 373 #endif 374 375 name = get_config_value_node(nvl, "name"); 376 if (name == NULL) { 377 EPRINTLN("vtcon: required name missing for port %ld", port); 378 error = -1; 379 goto out; 380 } 381 sock->vss_port = pci_vtcon_port_add(sc, port, name, pci_vtcon_sock_tx, sock); 382 if (sock->vss_port == NULL) { 383 error = -1; 384 goto out; 385 } 386 387 sock->vss_open = false; 388 sock->vss_conn_fd = -1; 389 sock->vss_server_fd = s; 390 sock->vss_server_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_accept, 391 sock); 392 393 if (sock->vss_server_evp == NULL) { 394 error = -1; 395 goto out; 396 } 397 398 out: 399 if (fd != -1) 400 close(fd); 401 402 if (error != 0) { 403 if (s != -1) 404 close(s); 405 free(sock); 406 } 407 408 return (error); 409 } 410 411 static void 412 pci_vtcon_sock_accept(int fd __unused, enum ev_type t __unused, void *arg) 413 { 414 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg; 415 int s; 416 417 s = accept(sock->vss_server_fd, NULL, NULL); 418 if (s < 0) 419 return; 420 421 if (sock->vss_open) { 422 close(s); 423 return; 424 } 425 426 sock->vss_open = true; 427 sock->vss_conn_fd = s; 428 sock->vss_conn_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_rx, sock); 429 430 pci_vtcon_open_port(sock->vss_port, true); 431 } 432 433 static void 434 pci_vtcon_sock_rx(int fd __unused, enum ev_type t __unused, void *arg) 435 { 436 struct pci_vtcon_port *port; 437 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg; 438 struct vqueue_info *vq; 439 struct iovec iov; 440 static char dummybuf[2048]; 441 int len, n; 442 uint16_t idx; 443 444 port = sock->vss_port; 445 vq = pci_vtcon_port_to_vq(port, true); 446 447 if (!sock->vss_open || !port->vsp_rx_ready) { 448 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf)); 449 if (len == 0) 450 goto close; 451 452 return; 453 } 454 455 if (!vq_has_descs(vq)) { 456 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf)); 457 vq_endchains(vq, 1); 458 if (len == 0) 459 goto close; 460 461 return; 462 } 463 464 do { 465 n = vq_getchain(vq, &idx, &iov, 1, NULL); 466 len = readv(sock->vss_conn_fd, &iov, n); 467 468 if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) { 469 vq_retchains(vq, 1); 470 vq_endchains(vq, 0); 471 if (len == 0) 472 goto close; 473 474 return; 475 } 476 477 vq_relchain(vq, idx, len); 478 } while (vq_has_descs(vq)); 479 480 vq_endchains(vq, 1); 481 482 close: 483 mevent_delete_close(sock->vss_conn_evp); 484 sock->vss_conn_fd = -1; 485 sock->vss_open = false; 486 } 487 488 static void 489 pci_vtcon_sock_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov, 490 int niov) 491 { 492 struct pci_vtcon_sock *sock; 493 int i, ret; 494 495 #ifndef __FreeBSD__ 496 ret = 0; 497 #endif 498 499 sock = (struct pci_vtcon_sock *)arg; 500 501 if (sock->vss_conn_fd == -1) 502 return; 503 504 for (i = 0; i < niov; i++) { 505 ret = stream_write(sock->vss_conn_fd, iov[i].iov_base, 506 iov[i].iov_len); 507 if (ret <= 0) 508 break; 509 } 510 511 if (ret <= 0) { 512 mevent_delete_close(sock->vss_conn_evp); 513 sock->vss_conn_fd = -1; 514 sock->vss_open = false; 515 } 516 } 517 518 static void 519 pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov, 520 int niov) 521 { 522 struct pci_vtcon_softc *sc; 523 struct pci_vtcon_port *tmp; 524 struct pci_vtcon_control resp, *ctrl; 525 int i; 526 527 assert(niov == 1); 528 529 sc = port->vsp_sc; 530 ctrl = (struct pci_vtcon_control *)iov->iov_base; 531 532 switch (ctrl->event) { 533 case VTCON_DEVICE_READY: 534 sc->vsc_ready = true; 535 /* set port ready events for registered ports */ 536 for (i = 0; i < VTCON_MAXPORTS; i++) { 537 tmp = &sc->vsc_ports[i]; 538 if (tmp->vsp_enabled) 539 pci_vtcon_announce_port(tmp); 540 541 if (tmp->vsp_open) 542 pci_vtcon_open_port(tmp, true); 543 } 544 break; 545 546 case VTCON_PORT_READY: 547 tmp = &sc->vsc_ports[ctrl->id]; 548 if (ctrl->id >= VTCON_MAXPORTS || !tmp->vsp_enabled) { 549 WPRINTF(("VTCON_PORT_READY event for unknown port %d", 550 ctrl->id)); 551 return; 552 } 553 554 if (tmp->vsp_console) { 555 resp.event = VTCON_CONSOLE_PORT; 556 resp.id = ctrl->id; 557 resp.value = 1; 558 pci_vtcon_control_send(sc, &resp, NULL, 0); 559 } 560 break; 561 } 562 } 563 564 static void 565 pci_vtcon_announce_port(struct pci_vtcon_port *port) 566 { 567 struct pci_vtcon_control event; 568 569 event.id = port->vsp_id; 570 event.event = VTCON_DEVICE_ADD; 571 event.value = 1; 572 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0); 573 574 event.event = VTCON_PORT_NAME; 575 pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name, 576 strlen(port->vsp_name)); 577 } 578 579 static void 580 pci_vtcon_open_port(struct pci_vtcon_port *port, bool open) 581 { 582 struct pci_vtcon_control event; 583 584 if (!port->vsp_sc->vsc_ready) { 585 port->vsp_open = true; 586 return; 587 } 588 589 event.id = port->vsp_id; 590 event.event = VTCON_PORT_OPEN; 591 event.value = (int)open; 592 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0); 593 } 594 595 static void 596 pci_vtcon_control_send(struct pci_vtcon_softc *sc, 597 struct pci_vtcon_control *ctrl, const void *payload, size_t len) 598 { 599 struct vqueue_info *vq; 600 struct iovec iov; 601 uint16_t idx; 602 int n; 603 604 vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true); 605 606 if (!vq_has_descs(vq)) 607 return; 608 609 n = vq_getchain(vq, &idx, &iov, 1, NULL); 610 611 assert(n == 1); 612 613 memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control)); 614 if (payload != NULL && len > 0) 615 memcpy(iov.iov_base + sizeof(struct pci_vtcon_control), 616 payload, len); 617 618 vq_relchain(vq, idx, sizeof(struct pci_vtcon_control) + len); 619 vq_endchains(vq, 1); 620 } 621 622 623 static void 624 pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq) 625 { 626 struct pci_vtcon_softc *sc; 627 struct pci_vtcon_port *port; 628 struct iovec iov[1]; 629 uint16_t idx, n; 630 uint16_t flags[8]; 631 632 sc = vsc; 633 port = pci_vtcon_vq_to_port(sc, vq); 634 635 while (vq_has_descs(vq)) { 636 n = vq_getchain(vq, &idx, iov, 1, flags); 637 assert(n >= 1); 638 if (port != NULL) 639 port->vsp_cb(port, port->vsp_arg, iov, 1); 640 641 /* 642 * Release this chain and handle more 643 */ 644 vq_relchain(vq, idx, 0); 645 } 646 vq_endchains(vq, 1); /* Generate interrupt if appropriate. */ 647 } 648 649 static void 650 pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq) 651 { 652 struct pci_vtcon_softc *sc; 653 struct pci_vtcon_port *port; 654 655 sc = vsc; 656 port = pci_vtcon_vq_to_port(sc, vq); 657 658 if (!port->vsp_rx_ready) { 659 port->vsp_rx_ready = 1; 660 vq_kick_disable(vq); 661 } 662 } 663 664 #ifdef __FreeBSD__ 665 /* 666 * Each console device has a "port" node which contains nodes for 667 * each port. Ports are numbered starting at 0. 668 */ 669 static int 670 pci_vtcon_legacy_config_port(nvlist_t *nvl, int port, char *opt) 671 { 672 char *name, *path; 673 char node_name[sizeof("XX")]; 674 nvlist_t *port_nvl; 675 676 name = strsep(&opt, "="); 677 path = opt; 678 if (path == NULL) { 679 EPRINTLN("vtcon: port %s requires a path", name); 680 return (-1); 681 } 682 if (port >= VTCON_MAXPORTS) { 683 EPRINTLN("vtcon: too many ports"); 684 return (-1); 685 } 686 snprintf(node_name, sizeof(node_name), "%d", port); 687 port_nvl = create_relative_config_node(nvl, node_name); 688 set_config_value_node(port_nvl, "name", name); 689 set_config_value_node(port_nvl, "path", path); 690 return (0); 691 } 692 693 static int 694 pci_vtcon_legacy_config(nvlist_t *nvl, const char *opts) 695 { 696 char *opt, *str, *tofree; 697 nvlist_t *ports_nvl; 698 int error, port; 699 700 ports_nvl = create_relative_config_node(nvl, "port"); 701 tofree = str = strdup(opts); 702 error = 0; 703 port = 0; 704 while ((opt = strsep(&str, ",")) != NULL) { 705 error = pci_vtcon_legacy_config_port(ports_nvl, port, opt); 706 if (error) 707 break; 708 port++; 709 } 710 free(tofree); 711 return (error); 712 } 713 #endif 714 715 static int 716 pci_vtcon_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl) 717 { 718 struct pci_vtcon_softc *sc; 719 nvlist_t *ports_nvl; 720 int i; 721 722 sc = calloc(1, sizeof(struct pci_vtcon_softc)); 723 sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config)); 724 sc->vsc_config->max_nr_ports = VTCON_MAXPORTS; 725 sc->vsc_config->cols = 80; 726 sc->vsc_config->rows = 25; 727 728 vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues); 729 sc->vsc_vs.vs_mtx = &sc->vsc_mtx; 730 731 for (i = 0; i < VTCON_MAXQ; i++) { 732 sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ; 733 sc->vsc_queues[i].vq_notify = i % 2 == 0 734 ? pci_vtcon_notify_rx 735 : pci_vtcon_notify_tx; 736 } 737 738 /* initialize config space */ 739 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE); 740 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR); 741 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM); 742 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_CONSOLE); 743 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR); 744 745 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix())) 746 return (1); 747 vi_set_io_bar(&sc->vsc_vs, 0); 748 749 /* create control port */ 750 sc->vsc_control_port.vsp_sc = sc; 751 sc->vsc_control_port.vsp_txq = 2; 752 sc->vsc_control_port.vsp_rxq = 3; 753 sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx; 754 sc->vsc_control_port.vsp_enabled = true; 755 756 ports_nvl = find_relative_config_node(nvl, "port"); 757 if (ports_nvl != NULL) { 758 const char *name; 759 void *cookie; 760 int type; 761 762 cookie = NULL; 763 while ((name = nvlist_next(ports_nvl, &type, &cookie)) != 764 NULL) { 765 if (type != NV_TYPE_NVLIST) 766 continue; 767 768 if (pci_vtcon_sock_add(sc, name, 769 nvlist_get_nvlist(ports_nvl, name)) < 0) { 770 EPRINTLN("cannot create port %s: %s", 771 name, strerror(errno)); 772 return (1); 773 } 774 } 775 } 776 777 return (0); 778 } 779 780 struct pci_devemu pci_de_vcon = { 781 .pe_emu = "virtio-console", 782 .pe_init = pci_vtcon_init, 783 .pe_barwrite = vi_pci_write, 784 .pe_barread = vi_pci_read 785 }; 786 PCI_EMUL_SET(pci_de_vcon); 787