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