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