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