xref: /illumos-gate/usr/src/cmd/bhyve/pci_virtio_console.c (revision 154972aff898a787b38af3bab5b8d754b5a42447)
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 "debug.h"
68 #include "pci_emul.h"
69 #include "virtio.h"
70 #include "mevent.h"
71 #include "sockstream.h"
72 
73 #define	VTCON_RINGSZ	64
74 #define	VTCON_MAXPORTS	16
75 #define	VTCON_MAXQ	(VTCON_MAXPORTS * 2 + 2)
76 
77 #define	VTCON_DEVICE_READY	0
78 #define	VTCON_DEVICE_ADD	1
79 #define	VTCON_DEVICE_REMOVE	2
80 #define	VTCON_PORT_READY	3
81 #define	VTCON_CONSOLE_PORT	4
82 #define	VTCON_CONSOLE_RESIZE	5
83 #define	VTCON_PORT_OPEN		6
84 #define	VTCON_PORT_NAME		7
85 
86 #define	VTCON_F_SIZE		0
87 #define	VTCON_F_MULTIPORT	1
88 #define	VTCON_F_EMERG_WRITE	2
89 #define	VTCON_S_HOSTCAPS	\
90     (VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE)
91 
92 static int pci_vtcon_debug;
93 #define DPRINTF(params) if (pci_vtcon_debug) PRINTLN params
94 #define WPRINTF(params) PRINTLN params
95 
96 struct pci_vtcon_softc;
97 struct pci_vtcon_port;
98 struct pci_vtcon_config;
99 typedef void (pci_vtcon_cb_t)(struct pci_vtcon_port *, void *, struct iovec *,
100     int);
101 
102 struct pci_vtcon_port {
103 	struct pci_vtcon_softc * vsp_sc;
104 	int                      vsp_id;
105 	const char *             vsp_name;
106 	bool                     vsp_enabled;
107 	bool                     vsp_console;
108 	bool                     vsp_rx_ready;
109 	bool                     vsp_open;
110 	int                      vsp_rxq;
111 	int                      vsp_txq;
112 	void *                   vsp_arg;
113 	pci_vtcon_cb_t *         vsp_cb;
114 };
115 
116 struct pci_vtcon_sock
117 {
118 	struct pci_vtcon_port *  vss_port;
119 	const char *             vss_path;
120 	struct mevent *          vss_server_evp;
121 	struct mevent *          vss_conn_evp;
122 	int                      vss_server_fd;
123 	int                      vss_conn_fd;
124 	bool                     vss_open;
125 };
126 
127 struct pci_vtcon_softc {
128 	struct virtio_softc      vsc_vs;
129 	struct vqueue_info       vsc_queues[VTCON_MAXQ];
130 	pthread_mutex_t          vsc_mtx;
131 	uint64_t                 vsc_cfg;
132 	uint64_t                 vsc_features;
133 	char *                   vsc_rootdir;
134 	int                      vsc_kq;
135 	int                      vsc_nports;
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, const char *name,
250     pci_vtcon_cb_t *cb, void *arg)
251 {
252 	struct pci_vtcon_port *port;
253 
254 	if (sc->vsc_nports == VTCON_MAXPORTS) {
255 		errno = EBUSY;
256 		return (NULL);
257 	}
258 
259 	port = &sc->vsc_ports[sc->vsc_nports++];
260 	port->vsp_id = sc->vsc_nports - 1;
261 	port->vsp_sc = sc;
262 	port->vsp_name = name;
263 	port->vsp_cb = cb;
264 	port->vsp_arg = arg;
265 
266 	if (port->vsp_id == 0) {
267 		/* port0 */
268 		port->vsp_txq = 0;
269 		port->vsp_rxq = 1;
270 	} else {
271 		port->vsp_txq = sc->vsc_nports * 2;
272 		port->vsp_rxq = port->vsp_txq + 1;
273 	}
274 
275 	port->vsp_enabled = true;
276 	return (port);
277 }
278 
279 static int
280 pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *name,
281     const char *path)
282 {
283 	struct pci_vtcon_sock *sock;
284 #ifdef __FreeBSD__
285 	struct sockaddr_un sun;
286 	char *pathcopy;
287 #else
288 	/* Our compiler #defines 'sun' as '1'.  Awesome. */
289 	struct sockaddr_un addr;
290 #endif
291 	int s = -1, fd = -1, error = 0;
292 #ifndef WITHOUT_CAPSICUM
293 	cap_rights_t rights;
294 #endif
295 
296 	sock = calloc(1, sizeof(struct pci_vtcon_sock));
297 	if (sock == NULL) {
298 		error = -1;
299 		goto out;
300 	}
301 
302 	s = socket(AF_UNIX, SOCK_STREAM, 0);
303 	if (s < 0) {
304 		error = -1;
305 		goto out;
306 	}
307 
308 #ifdef __FreeBSD__
309 	pathcopy = strdup(path);
310 	if (pathcopy == NULL) {
311 		error = -1;
312 		goto out;
313 	}
314 
315 	fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY);
316 	if (fd < 0) {
317 		free(pathcopy);
318 		error = -1;
319 		goto out;
320 	}
321 
322 	sun.sun_family = AF_UNIX;
323 	sun.sun_len = sizeof(struct sockaddr_un);
324 	strcpy(pathcopy, path);
325 	strlcpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path));
326 	free(pathcopy);
327 
328 	if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) {
329 		error = -1;
330 		goto out;
331 	}
332 #else /* __FreeBSD__ */
333 	/* Do a simple bind rather than the FreeBSD bindat() */
334 	addr.sun_family = AF_UNIX;
335 	(void) strlcpy(addr.sun_path, path, sizeof (addr.sun_path));
336 	if (bind(fd, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
337 		error = -1;
338 		goto out;
339 	}
340 #endif /* __FreeBSD__ */
341 
342 	if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
343 		error = -1;
344 		goto out;
345 	}
346 
347 	if (listen(s, 1) < 0) {
348 		error = -1;
349 		goto out;
350 	}
351 
352 #ifndef WITHOUT_CAPSICUM
353 	cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE);
354 	if (caph_rights_limit(s, &rights) == -1)
355 		errx(EX_OSERR, "Unable to apply rights for sandbox");
356 #endif
357 
358 	sock->vss_port = pci_vtcon_port_add(sc, name, pci_vtcon_sock_tx, sock);
359 	if (sock->vss_port == NULL) {
360 		error = -1;
361 		goto out;
362 	}
363 
364 	sock->vss_open = false;
365 	sock->vss_conn_fd = -1;
366 	sock->vss_server_fd = s;
367 	sock->vss_server_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_accept,
368 	    sock);
369 
370 	if (sock->vss_server_evp == NULL) {
371 		error = -1;
372 		goto out;
373 	}
374 
375 out:
376 	if (fd != -1)
377 		close(fd);
378 
379 	if (error != 0) {
380 		if (s != -1)
381 			close(s);
382 		free(sock);
383 	}
384 
385 	return (error);
386 }
387 
388 static void
389 pci_vtcon_sock_accept(int fd __unused, enum ev_type t __unused, void *arg)
390 {
391 	struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
392 	int s;
393 
394 	s = accept(sock->vss_server_fd, NULL, NULL);
395 	if (s < 0)
396 		return;
397 
398 	if (sock->vss_open) {
399 		close(s);
400 		return;
401 	}
402 
403 	sock->vss_open = true;
404 	sock->vss_conn_fd = s;
405 	sock->vss_conn_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_rx, sock);
406 
407 	pci_vtcon_open_port(sock->vss_port, true);
408 }
409 
410 static void
411 pci_vtcon_sock_rx(int fd __unused, enum ev_type t __unused, void *arg)
412 {
413 	struct pci_vtcon_port *port;
414 	struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
415 	struct vqueue_info *vq;
416 	struct iovec iov;
417 	static char dummybuf[2048];
418 	int len, n;
419 	uint16_t idx;
420 
421 	port = sock->vss_port;
422 	vq = pci_vtcon_port_to_vq(port, true);
423 
424 	if (!sock->vss_open || !port->vsp_rx_ready) {
425 		len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
426 		if (len == 0)
427 			goto close;
428 
429 		return;
430 	}
431 
432 	if (!vq_has_descs(vq)) {
433 		len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
434 		vq_endchains(vq, 1);
435 		if (len == 0)
436 			goto close;
437 
438 		return;
439 	}
440 
441 	do {
442 		n = vq_getchain(vq, &idx, &iov, 1, NULL);
443 		len = readv(sock->vss_conn_fd, &iov, n);
444 
445 		if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) {
446 			vq_retchains(vq, 1);
447 			vq_endchains(vq, 0);
448 			if (len == 0)
449 				goto close;
450 
451 			return;
452 		}
453 
454 		vq_relchain(vq, idx, len);
455 	} while (vq_has_descs(vq));
456 
457 	vq_endchains(vq, 1);
458 
459 close:
460 	mevent_delete_close(sock->vss_conn_evp);
461 	sock->vss_conn_fd = -1;
462 	sock->vss_open = false;
463 }
464 
465 static void
466 pci_vtcon_sock_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
467     int niov)
468 {
469 	struct pci_vtcon_sock *sock;
470 #ifdef __FreeBSD__
471 	int i, ret;
472 #else
473 	int i, ret = 0;
474 #endif
475 
476 	sock = (struct pci_vtcon_sock *)arg;
477 
478 	if (sock->vss_conn_fd == -1)
479 		return;
480 
481 	for (i = 0; i < niov; i++) {
482 		ret = stream_write(sock->vss_conn_fd, iov[i].iov_base,
483 		    iov[i].iov_len);
484 		if (ret <= 0)
485 			break;
486 	}
487 
488 	if (ret <= 0) {
489 		mevent_delete_close(sock->vss_conn_evp);
490 		sock->vss_conn_fd = -1;
491 		sock->vss_open = false;
492 	}
493 }
494 
495 static void
496 pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
497     int niov)
498 {
499 	struct pci_vtcon_softc *sc;
500 	struct pci_vtcon_port *tmp;
501 	struct pci_vtcon_control resp, *ctrl;
502 	int i;
503 
504 	assert(niov == 1);
505 
506 	sc = port->vsp_sc;
507 	ctrl = (struct pci_vtcon_control *)iov->iov_base;
508 
509 	switch (ctrl->event) {
510 	case VTCON_DEVICE_READY:
511 		sc->vsc_ready = true;
512 		/* set port ready events for registered ports */
513 		for (i = 0; i < VTCON_MAXPORTS; i++) {
514 			tmp = &sc->vsc_ports[i];
515 			if (tmp->vsp_enabled)
516 				pci_vtcon_announce_port(tmp);
517 
518 			if (tmp->vsp_open)
519 				pci_vtcon_open_port(tmp, true);
520 		}
521 		break;
522 
523 	case VTCON_PORT_READY:
524 		if (ctrl->id >= sc->vsc_nports) {
525 			WPRINTF(("VTCON_PORT_READY event for unknown port %d",
526 			    ctrl->id));
527 			return;
528 		}
529 
530 		tmp = &sc->vsc_ports[ctrl->id];
531 		if (tmp->vsp_console) {
532 			resp.event = VTCON_CONSOLE_PORT;
533 			resp.id = ctrl->id;
534 			resp.value = 1;
535 			pci_vtcon_control_send(sc, &resp, NULL, 0);
536 		}
537 		break;
538 	}
539 }
540 
541 static void
542 pci_vtcon_announce_port(struct pci_vtcon_port *port)
543 {
544 	struct pci_vtcon_control event;
545 
546 	event.id = port->vsp_id;
547 	event.event = VTCON_DEVICE_ADD;
548 	event.value = 1;
549 	pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
550 
551 	event.event = VTCON_PORT_NAME;
552 	pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name,
553 	    strlen(port->vsp_name));
554 }
555 
556 static void
557 pci_vtcon_open_port(struct pci_vtcon_port *port, bool open)
558 {
559 	struct pci_vtcon_control event;
560 
561 	if (!port->vsp_sc->vsc_ready) {
562 		port->vsp_open = true;
563 		return;
564 	}
565 
566 	event.id = port->vsp_id;
567 	event.event = VTCON_PORT_OPEN;
568 	event.value = (int)open;
569 	pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
570 }
571 
572 static void
573 pci_vtcon_control_send(struct pci_vtcon_softc *sc,
574     struct pci_vtcon_control *ctrl, const void *payload, size_t len)
575 {
576 	struct vqueue_info *vq;
577 	struct iovec iov;
578 	uint16_t idx;
579 	int n;
580 
581 	vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true);
582 
583 	if (!vq_has_descs(vq))
584 		return;
585 
586 	n = vq_getchain(vq, &idx, &iov, 1, NULL);
587 
588 	assert(n == 1);
589 
590 	memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control));
591 	if (payload != NULL && len > 0)
592 		memcpy(iov.iov_base + sizeof(struct pci_vtcon_control),
593 		     payload, len);
594 
595 	vq_relchain(vq, idx, sizeof(struct pci_vtcon_control) + len);
596 	vq_endchains(vq, 1);
597 }
598 
599 
600 static void
601 pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq)
602 {
603 	struct pci_vtcon_softc *sc;
604 	struct pci_vtcon_port *port;
605 	struct iovec iov[1];
606 	uint16_t idx, n;
607 	uint16_t flags[8];
608 
609 	sc = vsc;
610 	port = pci_vtcon_vq_to_port(sc, vq);
611 
612 	while (vq_has_descs(vq)) {
613 		n = vq_getchain(vq, &idx, iov, 1, flags);
614 		assert(n >= 1);
615 		if (port != NULL)
616 			port->vsp_cb(port, port->vsp_arg, iov, 1);
617 
618 		/*
619 		 * Release this chain and handle more
620 		 */
621 		vq_relchain(vq, idx, 0);
622 	}
623 	vq_endchains(vq, 1);	/* Generate interrupt if appropriate. */
624 }
625 
626 static void
627 pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq)
628 {
629 	struct pci_vtcon_softc *sc;
630 	struct pci_vtcon_port *port;
631 
632 	sc = vsc;
633 	port = pci_vtcon_vq_to_port(sc, vq);
634 
635 	if (!port->vsp_rx_ready) {
636 		port->vsp_rx_ready = 1;
637 		vq_kick_disable(vq);
638 	}
639 }
640 
641 static int
642 pci_vtcon_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
643 {
644 	struct pci_vtcon_softc *sc;
645 	char *portname = NULL;
646 	char *portpath = NULL;
647 	char *opt;
648 	int i;
649 
650 	sc = calloc(1, sizeof(struct pci_vtcon_softc));
651 	sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config));
652 	sc->vsc_config->max_nr_ports = VTCON_MAXPORTS;
653 	sc->vsc_config->cols = 80;
654 	sc->vsc_config->rows = 25;
655 
656 	vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues);
657 	sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
658 
659 	for (i = 0; i < VTCON_MAXQ; i++) {
660 		sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ;
661 		sc->vsc_queues[i].vq_notify = i % 2 == 0
662 		    ? pci_vtcon_notify_rx
663 		    : pci_vtcon_notify_tx;
664 	}
665 
666 	/* initialize config space */
667 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE);
668 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
669 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM);
670 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_CONSOLE);
671 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
672 
673 	if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
674 		return (1);
675 	vi_set_io_bar(&sc->vsc_vs, 0);
676 
677 	/* create control port */
678 	sc->vsc_control_port.vsp_sc = sc;
679 	sc->vsc_control_port.vsp_txq = 2;
680 	sc->vsc_control_port.vsp_rxq = 3;
681 	sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx;
682 	sc->vsc_control_port.vsp_enabled = true;
683 
684 	while ((opt = strsep(&opts, ",")) != NULL) {
685 		portname = strsep(&opt, "=");
686 		portpath = opt;
687 
688 		/* create port */
689 		if (pci_vtcon_sock_add(sc, portname, portpath) < 0) {
690 			EPRINTLN("cannot create port %s: %s",
691 			    portname, strerror(errno));
692 			return (1);
693 		}
694 	}
695 
696 	return (0);
697 }
698 
699 struct pci_devemu pci_de_vcon = {
700 	.pe_emu =	"virtio-console",
701 	.pe_init =	pci_vtcon_init,
702 	.pe_barwrite =	vi_pci_write,
703 	.pe_barread =	vi_pci_read
704 };
705 PCI_EMUL_SET(pci_de_vcon);
706