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