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 	.vc_name =	"vtcon",
173 	.vc_nvq =	VTCON_MAXQ,
174 	.vc_cfgsize =	sizeof(struct pci_vtcon_config),
175 	.vc_reset =	pci_vtcon_reset,
176 	.vc_cfgread =	pci_vtcon_cfgread,
177 	.vc_cfgwrite =	pci_vtcon_cfgwrite,
178 	.vc_apply_features = pci_vtcon_neg_features,
179 	.vc_hv_caps =	VTCON_S_HOSTCAPS,
180 };
181 
182 static void
183 pci_vtcon_reset(void *vsc)
184 {
185 	struct pci_vtcon_softc *sc;
186 
187 	sc = vsc;
188 
189 	DPRINTF(("vtcon: device reset requested!"));
190 	vi_reset_dev(&sc->vsc_vs);
191 }
192 
193 static void
194 pci_vtcon_neg_features(void *vsc, uint64_t negotiated_features)
195 {
196 	struct pci_vtcon_softc *sc = vsc;
197 
198 	sc->vsc_features = negotiated_features;
199 }
200 
201 static int
202 pci_vtcon_cfgread(void *vsc, int offset, int size, uint32_t *retval)
203 {
204 	struct pci_vtcon_softc *sc = vsc;
205 	void *ptr;
206 
207 	ptr = (uint8_t *)sc->vsc_config + offset;
208 	memcpy(retval, ptr, size);
209 	return (0);
210 }
211 
212 static int
213 pci_vtcon_cfgwrite(void *vsc __unused, int offset __unused, int size __unused,
214     uint32_t val __unused)
215 {
216 	return (0);
217 }
218 
219 static inline struct pci_vtcon_port *
220 pci_vtcon_vq_to_port(struct pci_vtcon_softc *sc, struct vqueue_info *vq)
221 {
222 	uint16_t num = vq->vq_num;
223 
224 	if (num == 0 || num == 1)
225 		return (&sc->vsc_ports[0]);
226 
227 	if (num == 2 || num == 3)
228 		return (&sc->vsc_control_port);
229 
230 	return (&sc->vsc_ports[(num / 2) - 1]);
231 }
232 
233 static inline struct vqueue_info *
234 pci_vtcon_port_to_vq(struct pci_vtcon_port *port, bool tx_queue)
235 {
236 	int qnum;
237 
238 	qnum = tx_queue ? port->vsp_txq : port->vsp_rxq;
239 	return (&port->vsp_sc->vsc_queues[qnum]);
240 }
241 
242 static struct pci_vtcon_port *
243 pci_vtcon_port_add(struct pci_vtcon_softc *sc, int port_id, const char *name,
244     pci_vtcon_cb_t *cb, void *arg)
245 {
246 	struct pci_vtcon_port *port;
247 
248 	port = &sc->vsc_ports[port_id];
249 	if (port->vsp_enabled) {
250 		errno = EBUSY;
251 		return (NULL);
252 	}
253 	port->vsp_id = port_id;
254 	port->vsp_sc = sc;
255 	port->vsp_name = name;
256 	port->vsp_cb = cb;
257 	port->vsp_arg = arg;
258 
259 	if (port->vsp_id == 0) {
260 		/* port0 */
261 		port->vsp_txq = 0;
262 		port->vsp_rxq = 1;
263 	} else {
264 		port->vsp_txq = (port_id + 1) * 2;
265 		port->vsp_rxq = port->vsp_txq + 1;
266 	}
267 
268 	port->vsp_enabled = true;
269 	return (port);
270 }
271 
272 static int
273 pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *port_name,
274     const nvlist_t *nvl)
275 {
276 	struct pci_vtcon_sock *sock = NULL;
277 	struct sockaddr_un sun;
278 	const char *name, *path;
279 	char *cp, *pathcopy;
280 	long port;
281 	int s = -1, fd = -1, error = 0;
282 #ifndef WITHOUT_CAPSICUM
283 	cap_rights_t rights;
284 #endif
285 
286 	port = strtol(port_name, &cp, 0);
287 	if (*cp != '\0' || port < 0 || port >= VTCON_MAXPORTS) {
288 		EPRINTLN("vtcon: Invalid port %s", port_name);
289 		error = -1;
290 		goto out;
291 	}
292 
293 	path = get_config_value_node(nvl, "path");
294 	if (path == NULL) {
295 		EPRINTLN("vtcon: required path missing for port %ld", port);
296 		error = -1;
297 		goto out;
298 	}
299 
300 	sock = calloc(1, sizeof(struct pci_vtcon_sock));
301 	if (sock == NULL) {
302 		error = -1;
303 		goto out;
304 	}
305 
306 	s = socket(AF_UNIX, SOCK_STREAM, 0);
307 	if (s < 0) {
308 		error = -1;
309 		goto out;
310 	}
311 
312 	pathcopy = strdup(path);
313 	if (pathcopy == NULL) {
314 		error = -1;
315 		goto out;
316 	}
317 
318 	fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY);
319 	if (fd < 0) {
320 		free(pathcopy);
321 		error = -1;
322 		goto out;
323 	}
324 
325 	sun.sun_family = AF_UNIX;
326 	sun.sun_len = sizeof(struct sockaddr_un);
327 	strcpy(pathcopy, path);
328 	strlcpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path));
329 	free(pathcopy);
330 
331 	if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) {
332 		error = -1;
333 		goto out;
334 	}
335 
336 	if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
337 		error = -1;
338 		goto out;
339 	}
340 
341 	if (listen(s, 1) < 0) {
342 		error = -1;
343 		goto out;
344 	}
345 
346 #ifndef WITHOUT_CAPSICUM
347 	cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE);
348 	if (caph_rights_limit(s, &rights) == -1)
349 		errx(EX_OSERR, "Unable to apply rights for sandbox");
350 #endif
351 
352 	name = get_config_value_node(nvl, "name");
353 	if (name == NULL) {
354 		EPRINTLN("vtcon: required name missing for port %ld", port);
355 		error = -1;
356 		goto out;
357 	}
358 	sock->vss_port = pci_vtcon_port_add(sc, port, 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 vi_req req;
417 	struct iovec iov;
418 	static char dummybuf[2048];
419 	int len, n;
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, &iov, 1, &req);
443 		assert(n == 1);
444 		len = readv(sock->vss_conn_fd, &iov, n);
445 
446 		if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) {
447 			vq_retchains(vq, 1);
448 			vq_endchains(vq, 0);
449 			if (len == 0)
450 				goto close;
451 
452 			return;
453 		}
454 
455 		vq_relchain(vq, req.idx, len);
456 	} while (vq_has_descs(vq));
457 
458 	vq_endchains(vq, 1);
459 
460 close:
461 	mevent_delete_close(sock->vss_conn_evp);
462 	sock->vss_conn_fd = -1;
463 	sock->vss_open = false;
464 }
465 
466 static void
467 pci_vtcon_sock_tx(struct pci_vtcon_port *port __unused, void *arg __unused,
468     struct iovec *iov, int niov)
469 {
470 	struct pci_vtcon_sock *sock;
471 	int i, ret;
472 
473 	sock = (struct pci_vtcon_sock *)arg;
474 
475 	if (sock->vss_conn_fd == -1)
476 		return;
477 
478 	for (i = 0; i < niov; i++) {
479 		ret = stream_write(sock->vss_conn_fd, iov[i].iov_base,
480 		    iov[i].iov_len);
481 		if (ret <= 0)
482 			break;
483 	}
484 
485 	if (ret <= 0) {
486 		mevent_delete_close(sock->vss_conn_evp);
487 		sock->vss_conn_fd = -1;
488 		sock->vss_open = false;
489 	}
490 }
491 
492 static void
493 pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg __unused,
494     struct iovec *iov, int niov)
495 {
496 	struct pci_vtcon_softc *sc;
497 	struct pci_vtcon_port *tmp;
498 	struct pci_vtcon_control resp, *ctrl;
499 	int i;
500 
501 	assert(niov == 1);
502 
503 	sc = port->vsp_sc;
504 	ctrl = (struct pci_vtcon_control *)iov->iov_base;
505 
506 	switch (ctrl->event) {
507 	case VTCON_DEVICE_READY:
508 		sc->vsc_ready = true;
509 		/* set port ready events for registered ports */
510 		for (i = 0; i < VTCON_MAXPORTS; i++) {
511 			tmp = &sc->vsc_ports[i];
512 			if (tmp->vsp_enabled)
513 				pci_vtcon_announce_port(tmp);
514 
515 			if (tmp->vsp_open)
516 				pci_vtcon_open_port(tmp, true);
517 		}
518 		break;
519 
520 	case VTCON_PORT_READY:
521 		tmp = &sc->vsc_ports[ctrl->id];
522 		if (ctrl->id >= VTCON_MAXPORTS || !tmp->vsp_enabled) {
523 			WPRINTF(("VTCON_PORT_READY event for unknown port %d",
524 			    ctrl->id));
525 			return;
526 		}
527 
528 		if (tmp->vsp_console) {
529 			resp.event = VTCON_CONSOLE_PORT;
530 			resp.id = ctrl->id;
531 			resp.value = 1;
532 			pci_vtcon_control_send(sc, &resp, NULL, 0);
533 		}
534 		break;
535 	}
536 }
537 
538 static void
539 pci_vtcon_announce_port(struct pci_vtcon_port *port)
540 {
541 	struct pci_vtcon_control event;
542 
543 	event.id = port->vsp_id;
544 	event.event = VTCON_DEVICE_ADD;
545 	event.value = 1;
546 	pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
547 
548 	event.event = VTCON_PORT_NAME;
549 	pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name,
550 	    strlen(port->vsp_name));
551 }
552 
553 static void
554 pci_vtcon_open_port(struct pci_vtcon_port *port, bool open)
555 {
556 	struct pci_vtcon_control event;
557 
558 	if (!port->vsp_sc->vsc_ready) {
559 		port->vsp_open = true;
560 		return;
561 	}
562 
563 	event.id = port->vsp_id;
564 	event.event = VTCON_PORT_OPEN;
565 	event.value = (int)open;
566 	pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
567 }
568 
569 static void
570 pci_vtcon_control_send(struct pci_vtcon_softc *sc,
571     struct pci_vtcon_control *ctrl, const void *payload, size_t len)
572 {
573 	struct vqueue_info *vq;
574 	struct vi_req req;
575 	struct iovec iov;
576 	int n;
577 
578 	vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true);
579 
580 	if (!vq_has_descs(vq))
581 		return;
582 
583 	n = vq_getchain(vq, &iov, 1, &req);
584 	assert(n == 1);
585 
586 	memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control));
587 	if (payload != NULL && len > 0)
588 		memcpy((uint8_t *)iov.iov_base +
589 		    sizeof(struct pci_vtcon_control), payload, len);
590 
591 	vq_relchain(vq, req.idx, sizeof(struct pci_vtcon_control) + len);
592 	vq_endchains(vq, 1);
593 }
594 
595 
596 static void
597 pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq)
598 {
599 	struct pci_vtcon_softc *sc;
600 	struct pci_vtcon_port *port;
601 	struct iovec iov[1];
602 	struct vi_req req;
603 	int n;
604 
605 	sc = vsc;
606 	port = pci_vtcon_vq_to_port(sc, vq);
607 
608 	while (vq_has_descs(vq)) {
609 		n = vq_getchain(vq, iov, 1, &req);
610 		assert(n == 1);
611 		if (port != NULL)
612 			port->vsp_cb(port, port->vsp_arg, iov, 1);
613 
614 		/*
615 		 * Release this chain and handle more
616 		 */
617 		vq_relchain(vq, req.idx, 0);
618 	}
619 	vq_endchains(vq, 1);	/* Generate interrupt if appropriate. */
620 }
621 
622 static void
623 pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq)
624 {
625 	struct pci_vtcon_softc *sc;
626 	struct pci_vtcon_port *port;
627 
628 	sc = vsc;
629 	port = pci_vtcon_vq_to_port(sc, vq);
630 
631 	if (!port->vsp_rx_ready) {
632 		port->vsp_rx_ready = 1;
633 		vq_kick_disable(vq);
634 	}
635 }
636 
637 /*
638  * Each console device has a "port" node which contains nodes for
639  * each port.  Ports are numbered starting at 0.
640  */
641 static int
642 pci_vtcon_legacy_config_port(nvlist_t *nvl, int port, char *opt)
643 {
644 	char *name, *path;
645 	char node_name[sizeof("XX")];
646 	nvlist_t *port_nvl;
647 
648 	name = strsep(&opt, "=");
649 	path = opt;
650 	if (path == NULL) {
651 		EPRINTLN("vtcon: port %s requires a path", name);
652 		return (-1);
653 	}
654 	if (port >= VTCON_MAXPORTS) {
655 		EPRINTLN("vtcon: too many ports");
656 		return (-1);
657 	}
658 	snprintf(node_name, sizeof(node_name), "%d", port);
659 	port_nvl = create_relative_config_node(nvl, node_name);
660 	set_config_value_node(port_nvl, "name", name);
661 	set_config_value_node(port_nvl, "path", path);
662 	return (0);
663 }
664 
665 static int
666 pci_vtcon_legacy_config(nvlist_t *nvl, const char *opts)
667 {
668 	char *opt, *str, *tofree;
669 	nvlist_t *ports_nvl;
670 	int error, port;
671 
672 	ports_nvl = create_relative_config_node(nvl, "port");
673 	tofree = str = strdup(opts);
674 	error = 0;
675 	port = 0;
676 	while ((opt = strsep(&str, ",")) != NULL) {
677 		error = pci_vtcon_legacy_config_port(ports_nvl, port, opt);
678 		if (error)
679 			break;
680 		port++;
681 	}
682 	free(tofree);
683 	return (error);
684 }
685 
686 static int
687 pci_vtcon_init(struct pci_devinst *pi, nvlist_t *nvl)
688 {
689 	struct pci_vtcon_softc *sc;
690 	nvlist_t *ports_nvl;
691 	int i;
692 
693 	sc = calloc(1, sizeof(struct pci_vtcon_softc));
694 	sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config));
695 	sc->vsc_config->max_nr_ports = VTCON_MAXPORTS;
696 	sc->vsc_config->cols = 80;
697 	sc->vsc_config->rows = 25;
698 
699 	pthread_mutex_init(&sc->vsc_mtx, NULL);
700 
701 	vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues);
702 	sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
703 
704 	for (i = 0; i < VTCON_MAXQ; i++) {
705 		sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ;
706 		sc->vsc_queues[i].vq_notify = i % 2 == 0
707 		    ? pci_vtcon_notify_rx
708 		    : pci_vtcon_notify_tx;
709 	}
710 
711 	/* initialize config space */
712 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE);
713 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
714 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM);
715 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_CONSOLE);
716 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
717 
718 	if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
719 		return (1);
720 	vi_set_io_bar(&sc->vsc_vs, 0);
721 
722 	/* create control port */
723 	sc->vsc_control_port.vsp_sc = sc;
724 	sc->vsc_control_port.vsp_txq = 2;
725 	sc->vsc_control_port.vsp_rxq = 3;
726 	sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx;
727 	sc->vsc_control_port.vsp_enabled = true;
728 
729 	ports_nvl = find_relative_config_node(nvl, "port");
730 	if (ports_nvl != NULL) {
731 		const char *name;
732 		void *cookie;
733 		int type;
734 
735 		cookie = NULL;
736 		while ((name = nvlist_next(ports_nvl, &type, &cookie)) !=
737 		    NULL) {
738 			if (type != NV_TYPE_NVLIST)
739 				continue;
740 
741 			if (pci_vtcon_sock_add(sc, name,
742 			    nvlist_get_nvlist(ports_nvl, name)) < 0) {
743 				EPRINTLN("cannot create port %s: %s",
744 				    name, strerror(errno));
745 				return (1);
746 			}
747 		}
748 	}
749 
750 	return (0);
751 }
752 
753 static const struct pci_devemu pci_de_vcon = {
754 	.pe_emu =	"virtio-console",
755 	.pe_init =	pci_vtcon_init,
756 	.pe_barwrite =	vi_pci_write,
757 	.pe_barread =	vi_pci_read,
758 	.pe_legacy_config = pci_vtcon_legacy_config,
759 };
760 PCI_EMUL_SET(pci_de_vcon);
761