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