1 /*-
2  * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /* Driver for VirtIO console devices. */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/ctype.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/kdb.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/sglist.h>
42 #include <sys/sysctl.h>
43 #include <sys/taskqueue.h>
44 #include <sys/queue.h>
45 
46 #include <sys/conf.h>
47 #include <sys/cons.h>
48 #include <sys/tty.h>
49 
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <sys/bus.h>
53 
54 #include <dev/virtio/virtio.h>
55 #include <dev/virtio/virtqueue.h>
56 #include <dev/virtio/console/virtio_console.h>
57 
58 #include "virtio_if.h"
59 
60 #define VTCON_MAX_PORTS 32
61 #define VTCON_TTY_PREFIX "V"
62 #define VTCON_TTY_ALIAS_PREFIX "vtcon"
63 #define VTCON_BULK_BUFSZ 128
64 #define VTCON_CTRL_BUFSZ 128
65 
66 /*
67  * The buffers cannot cross more than one page boundary due to the
68  * size of the sglist segment array used.
69  */
70 CTASSERT(VTCON_BULK_BUFSZ <= PAGE_SIZE);
71 CTASSERT(VTCON_CTRL_BUFSZ <= PAGE_SIZE);
72 
73 CTASSERT(sizeof(struct virtio_console_config) <= VTCON_CTRL_BUFSZ);
74 
75 struct vtcon_softc;
76 struct vtcon_softc_port;
77 
78 struct vtcon_port {
79 	struct mtx			 vtcport_mtx;
80 	struct vtcon_softc		*vtcport_sc;
81 	struct vtcon_softc_port		*vtcport_scport;
82 	struct tty			*vtcport_tty;
83 	struct virtqueue		*vtcport_invq;
84 	struct virtqueue		*vtcport_outvq;
85 	int				 vtcport_id;
86 	int				 vtcport_flags;
87 #define VTCON_PORT_FLAG_GONE	0x01
88 #define VTCON_PORT_FLAG_CONSOLE	0x02
89 #define VTCON_PORT_FLAG_ALIAS	0x04
90 
91 #if defined(KDB)
92 	int				 vtcport_alt_break_state;
93 #endif
94 };
95 
96 #define VTCON_PORT_LOCK(_port)		mtx_lock(&(_port)->vtcport_mtx)
97 #define VTCON_PORT_UNLOCK(_port)	mtx_unlock(&(_port)->vtcport_mtx)
98 
99 struct vtcon_softc_port {
100 	struct vtcon_softc	*vcsp_sc;
101 	struct vtcon_port	*vcsp_port;
102 	struct virtqueue	*vcsp_invq;
103 	struct virtqueue	*vcsp_outvq;
104 };
105 
106 struct vtcon_softc {
107 	device_t		 vtcon_dev;
108 	struct mtx		 vtcon_mtx;
109 	uint64_t		 vtcon_features;
110 	uint32_t		 vtcon_max_ports;
111 	uint32_t		 vtcon_flags;
112 #define VTCON_FLAG_DETACHED	0x01
113 #define VTCON_FLAG_SIZE		0x02
114 #define VTCON_FLAG_MULTIPORT	0x04
115 
116 	/*
117 	 * Ports can be added and removed during runtime, but we have
118 	 * to allocate all the virtqueues during attach. This array is
119 	 * indexed by the port ID.
120 	 */
121 	struct vtcon_softc_port	*vtcon_ports;
122 
123 	struct task		 vtcon_ctrl_task;
124 	struct virtqueue	*vtcon_ctrl_rxvq;
125 	struct virtqueue	*vtcon_ctrl_txvq;
126 	struct mtx		 vtcon_ctrl_tx_mtx;
127 };
128 
129 #define VTCON_LOCK(_sc)			mtx_lock(&(_sc)->vtcon_mtx)
130 #define VTCON_UNLOCK(_sc)		mtx_unlock(&(_sc)->vtcon_mtx)
131 #define VTCON_LOCK_ASSERT(_sc)		\
132     mtx_assert(&(_sc)->vtcon_mtx, MA_OWNED)
133 #define VTCON_LOCK_ASSERT_NOTOWNED(_sc)	\
134     mtx_assert(&(_sc)->vtcon_mtx, MA_NOTOWNED)
135 
136 #define VTCON_CTRL_TX_LOCK(_sc)		mtx_lock(&(_sc)->vtcon_ctrl_tx_mtx)
137 #define VTCON_CTRL_TX_UNLOCK(_sc)	mtx_unlock(&(_sc)->vtcon_ctrl_tx_mtx)
138 
139 #define VTCON_ASSERT_VALID_PORTID(_sc, _id)			\
140     KASSERT((_id) >= 0 && (_id) < (_sc)->vtcon_max_ports,	\
141         ("%s: port ID %d out of range", __func__, _id))
142 
143 #define VTCON_FEATURES  VIRTIO_CONSOLE_F_MULTIPORT
144 
145 static struct virtio_feature_desc vtcon_feature_desc[] = {
146 	{ VIRTIO_CONSOLE_F_SIZE,	"ConsoleSize"	},
147 	{ VIRTIO_CONSOLE_F_MULTIPORT,	"MultiplePorts"	},
148 	{ VIRTIO_CONSOLE_F_EMERG_WRITE,	"EmergencyWrite" },
149 
150 	{ 0, NULL }
151 };
152 
153 static int	 vtcon_modevent(module_t, int, void *);
154 static void	 vtcon_drain_all(void);
155 
156 static int	 vtcon_probe(device_t);
157 static int	 vtcon_attach(device_t);
158 static int	 vtcon_detach(device_t);
159 static int	 vtcon_config_change(device_t);
160 
161 static void	 vtcon_setup_features(struct vtcon_softc *);
162 static void	 vtcon_negotiate_features(struct vtcon_softc *);
163 static int	 vtcon_alloc_scports(struct vtcon_softc *);
164 static int	 vtcon_alloc_virtqueues(struct vtcon_softc *);
165 static void	 vtcon_read_config(struct vtcon_softc *,
166 		     struct virtio_console_config *);
167 
168 static void	 vtcon_determine_max_ports(struct vtcon_softc *,
169 		     struct virtio_console_config *);
170 static void	 vtcon_destroy_ports(struct vtcon_softc *);
171 static void	 vtcon_stop(struct vtcon_softc *);
172 
173 static int	 vtcon_ctrl_event_enqueue(struct vtcon_softc *,
174 		     struct virtio_console_control *);
175 static int	 vtcon_ctrl_event_create(struct vtcon_softc *);
176 static void	 vtcon_ctrl_event_requeue(struct vtcon_softc *,
177 		     struct virtio_console_control *);
178 static int	 vtcon_ctrl_event_populate(struct vtcon_softc *);
179 static void	 vtcon_ctrl_event_drain(struct vtcon_softc *);
180 static int	 vtcon_ctrl_init(struct vtcon_softc *);
181 static void	 vtcon_ctrl_deinit(struct vtcon_softc *);
182 static void	 vtcon_ctrl_port_add_event(struct vtcon_softc *, int);
183 static void	 vtcon_ctrl_port_remove_event(struct vtcon_softc *, int);
184 static void	 vtcon_ctrl_port_console_event(struct vtcon_softc *, int);
185 static void	 vtcon_ctrl_port_open_event(struct vtcon_softc *, int);
186 static void	 vtcon_ctrl_port_name_event(struct vtcon_softc *, int,
187 		     const char *, size_t);
188 static void	 vtcon_ctrl_process_event(struct vtcon_softc *,
189 		     struct virtio_console_control *, void *, size_t);
190 static void	 vtcon_ctrl_task_cb(void *, int);
191 static void	 vtcon_ctrl_event_intr(void *);
192 static void	 vtcon_ctrl_poll(struct vtcon_softc *,
193 		     struct virtio_console_control *control);
194 static void	 vtcon_ctrl_send_control(struct vtcon_softc *, uint32_t,
195 		     uint16_t, uint16_t);
196 
197 static int	 vtcon_port_enqueue_buf(struct vtcon_port *, void *, size_t);
198 static int	 vtcon_port_create_buf(struct vtcon_port *);
199 static void	 vtcon_port_requeue_buf(struct vtcon_port *, void *);
200 static int	 vtcon_port_populate(struct vtcon_port *);
201 static void	 vtcon_port_destroy(struct vtcon_port *);
202 static int	 vtcon_port_create(struct vtcon_softc *, int);
203 static void	 vtcon_port_dev_alias(struct vtcon_port *, const char *,
204 		     size_t);
205 static void	 vtcon_port_drain_bufs(struct virtqueue *);
206 static void	 vtcon_port_drain(struct vtcon_port *);
207 static void	 vtcon_port_teardown(struct vtcon_port *);
208 static void	 vtcon_port_change_size(struct vtcon_port *, uint16_t,
209 		     uint16_t);
210 static void	 vtcon_port_update_console_size(struct vtcon_softc *);
211 static void	 vtcon_port_enable_intr(struct vtcon_port *);
212 static void	 vtcon_port_disable_intr(struct vtcon_port *);
213 static void	 vtcon_port_in(struct vtcon_port *);
214 static void	 vtcon_port_intr(void *);
215 static void	 vtcon_port_out(struct vtcon_port *, void *, int);
216 static void	 vtcon_port_submit_event(struct vtcon_port *, uint16_t,
217 		     uint16_t);
218 
219 static int	 vtcon_tty_open(struct tty *);
220 static void	 vtcon_tty_close(struct tty *);
221 static void	 vtcon_tty_outwakeup(struct tty *);
222 static void	 vtcon_tty_free(void *);
223 
224 static void	 vtcon_get_console_size(struct vtcon_softc *, uint16_t *,
225 		     uint16_t *);
226 
227 static void	 vtcon_enable_interrupts(struct vtcon_softc *);
228 static void	 vtcon_disable_interrupts(struct vtcon_softc *);
229 
230 static int	 vtcon_pending_free;
231 
232 static struct ttydevsw vtcon_tty_class = {
233 	.tsw_flags	= 0,
234 	.tsw_open	= vtcon_tty_open,
235 	.tsw_close	= vtcon_tty_close,
236 	.tsw_outwakeup	= vtcon_tty_outwakeup,
237 	.tsw_free	= vtcon_tty_free,
238 };
239 
240 static device_method_t vtcon_methods[] = {
241 	/* Device methods. */
242 	DEVMETHOD(device_probe,		vtcon_probe),
243 	DEVMETHOD(device_attach,	vtcon_attach),
244 	DEVMETHOD(device_detach,	vtcon_detach),
245 
246 	/* VirtIO methods. */
247 	DEVMETHOD(virtio_config_change,	vtcon_config_change),
248 
249 	DEVMETHOD_END
250 };
251 
252 static driver_t vtcon_driver = {
253 	"vtcon",
254 	vtcon_methods,
255 	sizeof(struct vtcon_softc)
256 };
257 static devclass_t vtcon_devclass;
258 
259 DRIVER_MODULE(virtio_console, virtio_pci, vtcon_driver, vtcon_devclass,
260     vtcon_modevent, 0);
261 MODULE_VERSION(virtio_console, 1);
262 MODULE_DEPEND(virtio_console, virtio, 1, 1, 1);
263 
264 static int
265 vtcon_modevent(module_t mod, int type, void *unused)
266 {
267 	int error;
268 
269 	switch (type) {
270 	case MOD_LOAD:
271 		error = 0;
272 		break;
273 	case MOD_QUIESCE:
274 		error = 0;
275 		break;
276 	case MOD_UNLOAD:
277 		vtcon_drain_all();
278 		error = 0;
279 		break;
280 	case MOD_SHUTDOWN:
281 		error = 0;
282 		break;
283 	default:
284 		error = EOPNOTSUPP;
285 		break;
286 	}
287 
288 	return (error);
289 }
290 
291 static void
292 vtcon_drain_all(void)
293 {
294 	int first;
295 
296 	for (first = 1; vtcon_pending_free != 0; first = 0) {
297 		if (first != 0) {
298 			printf("virtio_console: Waiting for all detached TTY "
299 			    "devices to have open fds closed.\n");
300 		}
301 		pause("vtcondra", hz);
302 	}
303 }
304 
305 static int
306 vtcon_probe(device_t dev)
307 {
308 
309 	if (virtio_get_device_type(dev) != VIRTIO_ID_CONSOLE)
310 		return (ENXIO);
311 
312 	device_set_desc(dev, "VirtIO Console Adapter");
313 
314 	return (BUS_PROBE_DEFAULT);
315 }
316 
317 static int
318 vtcon_attach(device_t dev)
319 {
320 	struct vtcon_softc *sc;
321 	struct virtio_console_config concfg;
322 	int error;
323 
324 	sc = device_get_softc(dev);
325 	sc->vtcon_dev = dev;
326 
327 	mtx_init(&sc->vtcon_mtx, "vtconmtx", NULL, MTX_DEF);
328 	mtx_init(&sc->vtcon_ctrl_tx_mtx, "vtconctrlmtx", NULL, MTX_DEF);
329 
330 	virtio_set_feature_desc(dev, vtcon_feature_desc);
331 	vtcon_setup_features(sc);
332 
333 	vtcon_read_config(sc, &concfg);
334 	vtcon_determine_max_ports(sc, &concfg);
335 
336 	error = vtcon_alloc_scports(sc);
337 	if (error) {
338 		device_printf(dev, "cannot allocate softc port structures\n");
339 		goto fail;
340 	}
341 
342 	error = vtcon_alloc_virtqueues(sc);
343 	if (error) {
344 		device_printf(dev, "cannot allocate virtqueues\n");
345 		goto fail;
346 	}
347 
348 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
349 		TASK_INIT(&sc->vtcon_ctrl_task, 0, vtcon_ctrl_task_cb, sc);
350 		error = vtcon_ctrl_init(sc);
351 		if (error)
352 			goto fail;
353 	} else {
354 		error = vtcon_port_create(sc, 0);
355 		if (error)
356 			goto fail;
357 		if (sc->vtcon_flags & VTCON_FLAG_SIZE)
358 			vtcon_port_update_console_size(sc);
359 	}
360 
361 	error = virtio_setup_intr(dev, INTR_TYPE_TTY);
362 	if (error) {
363 		device_printf(dev, "cannot setup virtqueue interrupts\n");
364 		goto fail;
365 	}
366 
367 	vtcon_enable_interrupts(sc);
368 
369 	vtcon_ctrl_send_control(sc, VIRTIO_CONSOLE_BAD_ID,
370 	    VIRTIO_CONSOLE_DEVICE_READY, 1);
371 
372 fail:
373 	if (error)
374 		vtcon_detach(dev);
375 
376 	return (error);
377 }
378 
379 static int
380 vtcon_detach(device_t dev)
381 {
382 	struct vtcon_softc *sc;
383 
384 	sc = device_get_softc(dev);
385 
386 	VTCON_LOCK(sc);
387 	sc->vtcon_flags |= VTCON_FLAG_DETACHED;
388 	if (device_is_attached(dev))
389 		vtcon_stop(sc);
390 	VTCON_UNLOCK(sc);
391 
392 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
393 		taskqueue_drain(taskqueue_thread, &sc->vtcon_ctrl_task);
394 		vtcon_ctrl_deinit(sc);
395 	}
396 
397 	vtcon_destroy_ports(sc);
398 	mtx_destroy(&sc->vtcon_mtx);
399 	mtx_destroy(&sc->vtcon_ctrl_tx_mtx);
400 
401 	return (0);
402 }
403 
404 static int
405 vtcon_config_change(device_t dev)
406 {
407 	struct vtcon_softc *sc;
408 
409 	sc = device_get_softc(dev);
410 
411 	/*
412 	 * When the multiport feature is negotiated, all configuration
413 	 * changes are done through control virtqueue events.
414 	 */
415 	if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0) {
416 		if (sc->vtcon_flags & VTCON_FLAG_SIZE)
417 			vtcon_port_update_console_size(sc);
418 	}
419 
420 	return (0);
421 }
422 
423 static void
424 vtcon_negotiate_features(struct vtcon_softc *sc)
425 {
426 	device_t dev;
427 	uint64_t features;
428 
429 	dev = sc->vtcon_dev;
430 	features = VTCON_FEATURES;
431 
432 	sc->vtcon_features = virtio_negotiate_features(dev, features);
433 }
434 
435 static void
436 vtcon_setup_features(struct vtcon_softc *sc)
437 {
438 	device_t dev;
439 
440 	dev = sc->vtcon_dev;
441 
442 	vtcon_negotiate_features(sc);
443 
444 	if (virtio_with_feature(dev, VIRTIO_CONSOLE_F_SIZE))
445 		sc->vtcon_flags |= VTCON_FLAG_SIZE;
446 	if (virtio_with_feature(dev, VIRTIO_CONSOLE_F_MULTIPORT))
447 		sc->vtcon_flags |= VTCON_FLAG_MULTIPORT;
448 }
449 
450 #define VTCON_GET_CONFIG(_dev, _feature, _field, _cfg)			\
451 	if (virtio_with_feature(_dev, _feature)) {			\
452 		virtio_read_device_config(_dev,				\
453 		    offsetof(struct virtio_console_config, _field),	\
454 		    &(_cfg)->_field, sizeof((_cfg)->_field));		\
455 	}
456 
457 static void
458 vtcon_read_config(struct vtcon_softc *sc, struct virtio_console_config *concfg)
459 {
460 	device_t dev;
461 
462 	dev = sc->vtcon_dev;
463 
464 	bzero(concfg, sizeof(struct virtio_console_config));
465 
466 	VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_SIZE, cols, concfg);
467 	VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_SIZE, rows, concfg);
468 	VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_MULTIPORT, max_nr_ports, concfg);
469 }
470 
471 #undef VTCON_GET_CONFIG
472 
473 static int
474 vtcon_alloc_scports(struct vtcon_softc *sc)
475 {
476 	struct vtcon_softc_port *scport;
477 	int max, i;
478 
479 	max = sc->vtcon_max_ports;
480 
481 	sc->vtcon_ports = malloc(sizeof(struct vtcon_softc_port) * max,
482 	    M_DEVBUF, M_NOWAIT | M_ZERO);
483 	if (sc->vtcon_ports == NULL)
484 		return (ENOMEM);
485 
486 	for (i = 0; i < max; i++) {
487 		scport = &sc->vtcon_ports[i];
488 		scport->vcsp_sc = sc;
489 	}
490 
491 	return (0);
492 }
493 
494 static int
495 vtcon_alloc_virtqueues(struct vtcon_softc *sc)
496 {
497 	device_t dev;
498 	struct vq_alloc_info *info;
499 	struct vtcon_softc_port *scport;
500 	int i, idx, portidx, nvqs, error;
501 
502 	dev = sc->vtcon_dev;
503 
504 	nvqs = sc->vtcon_max_ports * 2;
505 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
506 		nvqs += 2;
507 
508 	info = malloc(sizeof(struct vq_alloc_info) * nvqs, M_TEMP, M_NOWAIT);
509 	if (info == NULL)
510 		return (ENOMEM);
511 
512 	for (i = 0, idx = 0, portidx = 0; i < nvqs / 2; i++, idx += 2) {
513 
514 		if (i == 1) {
515 			/* The control virtqueues are after the first port. */
516 			VQ_ALLOC_INFO_INIT(&info[idx], 0,
517 			    vtcon_ctrl_event_intr, sc, &sc->vtcon_ctrl_rxvq,
518 			    "%s-control rx", device_get_nameunit(dev));
519 			VQ_ALLOC_INFO_INIT(&info[idx+1], 0,
520 			    NULL, sc, &sc->vtcon_ctrl_txvq,
521 			    "%s-control tx", device_get_nameunit(dev));
522 			continue;
523 		}
524 
525 		scport = &sc->vtcon_ports[portidx];
526 
527 		VQ_ALLOC_INFO_INIT(&info[idx], 0, vtcon_port_intr,
528 		    scport, &scport->vcsp_invq, "%s-port%d in",
529 		    device_get_nameunit(dev), i);
530 		VQ_ALLOC_INFO_INIT(&info[idx+1], 0, NULL,
531 		    NULL, &scport->vcsp_outvq, "%s-port%d out",
532 		    device_get_nameunit(dev), i);
533 
534 		portidx++;
535 	}
536 
537 	error = virtio_alloc_virtqueues(dev, 0, nvqs, info);
538 	free(info, M_TEMP);
539 
540 	return (error);
541 }
542 
543 static void
544 vtcon_determine_max_ports(struct vtcon_softc *sc,
545     struct virtio_console_config *concfg)
546 {
547 
548 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
549 		sc->vtcon_max_ports =
550 		    min(concfg->max_nr_ports, VTCON_MAX_PORTS);
551 		if (sc->vtcon_max_ports == 0)
552 			sc->vtcon_max_ports = 1;
553 	} else
554 		sc->vtcon_max_ports = 1;
555 }
556 
557 static void
558 vtcon_destroy_ports(struct vtcon_softc *sc)
559 {
560 	struct vtcon_softc_port *scport;
561 	struct vtcon_port *port;
562 	struct virtqueue *vq;
563 	int i;
564 
565 	if (sc->vtcon_ports == NULL)
566 		return;
567 
568 	VTCON_LOCK(sc);
569 	for (i = 0; i < sc->vtcon_max_ports; i++) {
570 		scport = &sc->vtcon_ports[i];
571 
572 		port = scport->vcsp_port;
573 		if (port != NULL) {
574 			scport->vcsp_port = NULL;
575 			VTCON_PORT_LOCK(port);
576 			VTCON_UNLOCK(sc);
577 			vtcon_port_teardown(port);
578 			VTCON_LOCK(sc);
579 		}
580 
581 		vq = scport->vcsp_invq;
582 		if (vq != NULL)
583 			vtcon_port_drain_bufs(vq);
584 	}
585 	VTCON_UNLOCK(sc);
586 
587 	free(sc->vtcon_ports, M_DEVBUF);
588 	sc->vtcon_ports = NULL;
589 }
590 
591 static void
592 vtcon_stop(struct vtcon_softc *sc)
593 {
594 
595 	vtcon_disable_interrupts(sc);
596 	virtio_stop(sc->vtcon_dev);
597 }
598 
599 static int
600 vtcon_ctrl_event_enqueue(struct vtcon_softc *sc,
601     struct virtio_console_control *control)
602 {
603 	struct sglist_seg segs[2];
604 	struct sglist sg;
605 	struct virtqueue *vq;
606 	int error;
607 
608 	vq = sc->vtcon_ctrl_rxvq;
609 
610 	sglist_init(&sg, 2, segs);
611 	error = sglist_append(&sg, control, VTCON_CTRL_BUFSZ);
612 	KASSERT(error == 0, ("%s: error %d adding control to sglist",
613 	    __func__, error));
614 
615 	return (virtqueue_enqueue(vq, control, &sg, 0, sg.sg_nseg));
616 }
617 
618 static int
619 vtcon_ctrl_event_create(struct vtcon_softc *sc)
620 {
621 	struct virtio_console_control *control;
622 	int error;
623 
624 	control = malloc(VTCON_CTRL_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
625 	if (control == NULL)
626 		return (ENOMEM);
627 
628 	error = vtcon_ctrl_event_enqueue(sc, control);
629 	if (error)
630 		free(control, M_DEVBUF);
631 
632 	return (error);
633 }
634 
635 static void
636 vtcon_ctrl_event_requeue(struct vtcon_softc *sc,
637     struct virtio_console_control *control)
638 {
639 	int error;
640 
641 	bzero(control, VTCON_CTRL_BUFSZ);
642 
643 	error = vtcon_ctrl_event_enqueue(sc, control);
644 	KASSERT(error == 0,
645 	    ("%s: cannot requeue control buffer %d", __func__, error));
646 }
647 
648 static int
649 vtcon_ctrl_event_populate(struct vtcon_softc *sc)
650 {
651 	struct virtqueue *vq;
652 	int nbufs, error;
653 
654 	vq = sc->vtcon_ctrl_rxvq;
655 	error = ENOSPC;
656 
657 	for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
658 		error = vtcon_ctrl_event_create(sc);
659 		if (error)
660 			break;
661 	}
662 
663 	if (nbufs > 0) {
664 		virtqueue_notify(vq);
665 		error = 0;
666 	}
667 
668 	return (error);
669 }
670 
671 static void
672 vtcon_ctrl_event_drain(struct vtcon_softc *sc)
673 {
674 	struct virtio_console_control *control;
675 	struct virtqueue *vq;
676 	int last;
677 
678 	vq = sc->vtcon_ctrl_rxvq;
679 	last = 0;
680 
681 	if (vq == NULL)
682 		return;
683 
684 	VTCON_LOCK(sc);
685 	while ((control = virtqueue_drain(vq, &last)) != NULL)
686 		free(control, M_DEVBUF);
687 	VTCON_UNLOCK(sc);
688 }
689 
690 static int
691 vtcon_ctrl_init(struct vtcon_softc *sc)
692 {
693 	int error;
694 
695 	error = vtcon_ctrl_event_populate(sc);
696 
697 	return (error);
698 }
699 
700 static void
701 vtcon_ctrl_deinit(struct vtcon_softc *sc)
702 {
703 
704 	vtcon_ctrl_event_drain(sc);
705 }
706 
707 static void
708 vtcon_ctrl_port_add_event(struct vtcon_softc *sc, int id)
709 {
710 	device_t dev;
711 	int error;
712 
713 	dev = sc->vtcon_dev;
714 
715 	/* This single thread only way for ports to be created. */
716 	if (sc->vtcon_ports[id].vcsp_port != NULL) {
717 		device_printf(dev, "%s: adding port %d, but already exists\n",
718 		    __func__, id);
719 		return;
720 	}
721 
722 	error = vtcon_port_create(sc, id);
723 	if (error) {
724 		device_printf(dev, "%s: cannot create port %d: %d\n",
725 		    __func__, id, error);
726 		vtcon_ctrl_send_control(sc, id, VIRTIO_CONSOLE_PORT_READY, 0);
727 		return;
728 	}
729 }
730 
731 static void
732 vtcon_ctrl_port_remove_event(struct vtcon_softc *sc, int id)
733 {
734 	device_t dev;
735 	struct vtcon_softc_port *scport;
736 	struct vtcon_port *port;
737 
738 	dev = sc->vtcon_dev;
739 	scport = &sc->vtcon_ports[id];
740 
741 	VTCON_LOCK(sc);
742 	port = scport->vcsp_port;
743 	if (port == NULL) {
744 		VTCON_UNLOCK(sc);
745 		device_printf(dev, "%s: remove port %d, but does not exist\n",
746 		    __func__, id);
747 		return;
748 	}
749 
750 	scport->vcsp_port = NULL;
751 	VTCON_PORT_LOCK(port);
752 	VTCON_UNLOCK(sc);
753 	vtcon_port_teardown(port);
754 }
755 
756 static void
757 vtcon_ctrl_port_console_event(struct vtcon_softc *sc, int id)
758 {
759 	device_t dev;
760 	struct vtcon_softc_port *scport;
761 	struct vtcon_port *port;
762 
763 	dev = sc->vtcon_dev;
764 	scport = &sc->vtcon_ports[id];
765 
766 	VTCON_LOCK(sc);
767 	port = scport->vcsp_port;
768 	if (port == NULL) {
769 		VTCON_UNLOCK(sc);
770 		device_printf(dev, "%s: console port %d, but does not exist\n",
771 		    __func__, id);
772 		return;
773 	}
774 
775 	VTCON_PORT_LOCK(port);
776 	VTCON_UNLOCK(sc);
777 	port->vtcport_flags |= VTCON_PORT_FLAG_CONSOLE;
778 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
779 	VTCON_PORT_UNLOCK(port);
780 }
781 
782 static void
783 vtcon_ctrl_port_open_event(struct vtcon_softc *sc, int id)
784 {
785 	device_t dev;
786 	struct vtcon_softc_port *scport;
787 	struct vtcon_port *port;
788 
789 	dev = sc->vtcon_dev;
790 	scport = &sc->vtcon_ports[id];
791 
792 	VTCON_LOCK(sc);
793 	port = scport->vcsp_port;
794 	if (port == NULL) {
795 		VTCON_UNLOCK(sc);
796 		device_printf(dev, "%s: open port %d, but does not exist\n",
797 		    __func__, id);
798 		return;
799 	}
800 
801 	VTCON_PORT_LOCK(port);
802 	VTCON_UNLOCK(sc);
803 	vtcon_port_enable_intr(port);
804 	VTCON_PORT_UNLOCK(port);
805 }
806 
807 static void
808 vtcon_ctrl_port_name_event(struct vtcon_softc *sc, int id, const char *name,
809     size_t len)
810 {
811 	device_t dev;
812 	struct vtcon_softc_port *scport;
813 	struct vtcon_port *port;
814 
815 	dev = sc->vtcon_dev;
816 	scport = &sc->vtcon_ports[id];
817 
818 	/*
819 	 * The VirtIO specification says the NUL terminator is not included in
820 	 * the length, but QEMU includes it. Adjust the length if needed.
821 	 */
822 	if (name == NULL || len == 0)
823 		return;
824 	if (name[len - 1] == '\0') {
825 		len--;
826 		if (len == 0)
827 			return;
828 	}
829 
830 	VTCON_LOCK(sc);
831 	port = scport->vcsp_port;
832 	if (port == NULL) {
833 		VTCON_UNLOCK(sc);
834 		device_printf(dev, "%s: name port %d, but does not exist\n",
835 		    __func__, id);
836 		return;
837 	}
838 
839 	VTCON_PORT_LOCK(port);
840 	VTCON_UNLOCK(sc);
841 	vtcon_port_dev_alias(port, name, len);
842 	VTCON_PORT_UNLOCK(port);
843 }
844 
845 static void
846 vtcon_ctrl_process_event(struct vtcon_softc *sc,
847     struct virtio_console_control *control, void *data, size_t data_len)
848 {
849 	device_t dev;
850 	int id;
851 
852 	dev = sc->vtcon_dev;
853 	id = control->id;
854 
855 	if (id < 0 || id >= sc->vtcon_max_ports) {
856 		device_printf(dev, "%s: invalid port ID %d\n", __func__, id);
857 		return;
858 	}
859 
860 	switch (control->event) {
861 	case VIRTIO_CONSOLE_PORT_ADD:
862 		vtcon_ctrl_port_add_event(sc, id);
863 		break;
864 
865 	case VIRTIO_CONSOLE_PORT_REMOVE:
866 		vtcon_ctrl_port_remove_event(sc, id);
867 		break;
868 
869 	case VIRTIO_CONSOLE_CONSOLE_PORT:
870 		vtcon_ctrl_port_console_event(sc, id);
871 		break;
872 
873 	case VIRTIO_CONSOLE_RESIZE:
874 		break;
875 
876 	case VIRTIO_CONSOLE_PORT_OPEN:
877 		vtcon_ctrl_port_open_event(sc, id);
878 		break;
879 
880 	case VIRTIO_CONSOLE_PORT_NAME:
881 		vtcon_ctrl_port_name_event(sc, id, (const char *)data, data_len);
882 		break;
883 	}
884 }
885 
886 static void
887 vtcon_ctrl_task_cb(void *xsc, int pending)
888 {
889 	struct vtcon_softc *sc;
890 	struct virtqueue *vq;
891 	struct virtio_console_control *control;
892 	void *data;
893 	size_t data_len;
894 	int detached;
895 	uint32_t len;
896 
897 	sc = xsc;
898 	vq = sc->vtcon_ctrl_rxvq;
899 
900 	VTCON_LOCK(sc);
901 
902 	while ((detached = (sc->vtcon_flags & VTCON_FLAG_DETACHED)) == 0) {
903 		control = virtqueue_dequeue(vq, &len);
904 		if (control == NULL)
905 			break;
906 
907 		if (len > sizeof(struct virtio_console_control)) {
908 			data = (void *) &control[1];
909 			data_len = len - sizeof(struct virtio_console_control);
910 		} else {
911 			data = NULL;
912 			data_len = 0;
913 		}
914 
915 		VTCON_UNLOCK(sc);
916 		vtcon_ctrl_process_event(sc, control, data, data_len);
917 		VTCON_LOCK(sc);
918 		vtcon_ctrl_event_requeue(sc, control);
919 	}
920 
921 	if (!detached) {
922 		virtqueue_notify(vq);
923 		if (virtqueue_enable_intr(vq) != 0)
924 			taskqueue_enqueue(taskqueue_thread,
925 			    &sc->vtcon_ctrl_task);
926 	}
927 
928 	VTCON_UNLOCK(sc);
929 }
930 
931 static void
932 vtcon_ctrl_event_intr(void *xsc)
933 {
934 	struct vtcon_softc *sc;
935 
936 	sc = xsc;
937 
938 	/*
939 	 * Only some events require us to potentially block, but it
940 	 * easier to just defer all event handling to the taskqueue.
941 	 */
942 	taskqueue_enqueue(taskqueue_thread, &sc->vtcon_ctrl_task);
943 }
944 
945 static void
946 vtcon_ctrl_poll(struct vtcon_softc *sc,
947     struct virtio_console_control *control)
948 {
949 	struct sglist_seg segs[2];
950 	struct sglist sg;
951 	struct virtqueue *vq;
952 	int error;
953 
954 	vq = sc->vtcon_ctrl_txvq;
955 
956 	sglist_init(&sg, 2, segs);
957 	error = sglist_append(&sg, control,
958 	    sizeof(struct virtio_console_control));
959 	KASSERT(error == 0, ("%s: error %d adding control to sglist",
960 	    __func__, error));
961 
962 	/*
963 	 * We cannot use the softc lock to serialize access to this
964 	 * virtqueue since this is called from the tty layer with the
965 	 * port lock held. Acquiring the softc would violate our lock
966 	 * ordering.
967 	 */
968 	VTCON_CTRL_TX_LOCK(sc);
969 	KASSERT(virtqueue_empty(vq),
970 	    ("%s: virtqueue is not emtpy", __func__));
971 	error = virtqueue_enqueue(vq, control, &sg, sg.sg_nseg, 0);
972 	if (error == 0) {
973 		virtqueue_notify(vq);
974 		virtqueue_poll(vq, NULL);
975 	}
976 	VTCON_CTRL_TX_UNLOCK(sc);
977 }
978 
979 static void
980 vtcon_ctrl_send_control(struct vtcon_softc *sc, uint32_t portid,
981     uint16_t event, uint16_t value)
982 {
983 	struct virtio_console_control control;
984 
985 	if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0)
986 		return;
987 
988 	control.id = portid;
989 	control.event = event;
990 	control.value = value;
991 
992 	vtcon_ctrl_poll(sc, &control);
993 }
994 
995 static int
996 vtcon_port_enqueue_buf(struct vtcon_port *port, void *buf, size_t len)
997 {
998 	struct sglist_seg segs[2];
999 	struct sglist sg;
1000 	struct virtqueue *vq;
1001 	int error;
1002 
1003 	vq = port->vtcport_invq;
1004 
1005 	sglist_init(&sg, 2, segs);
1006 	error = sglist_append(&sg, buf, len);
1007 	KASSERT(error == 0,
1008 	    ("%s: error %d adding buffer to sglist", __func__, error));
1009 
1010 	error = virtqueue_enqueue(vq, buf, &sg, 0, sg.sg_nseg);
1011 
1012 	return (error);
1013 }
1014 
1015 static int
1016 vtcon_port_create_buf(struct vtcon_port *port)
1017 {
1018 	void *buf;
1019 	int error;
1020 
1021 	buf = malloc(VTCON_BULK_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
1022 	if (buf == NULL)
1023 		return (ENOMEM);
1024 
1025 	error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1026 	if (error)
1027 		free(buf, M_DEVBUF);
1028 
1029 	return (error);
1030 }
1031 
1032 static void
1033 vtcon_port_requeue_buf(struct vtcon_port *port, void *buf)
1034 {
1035 	int error;
1036 
1037 	error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1038 	KASSERT(error == 0,
1039 	    ("%s: cannot requeue input buffer %d", __func__, error));
1040 }
1041 
1042 static int
1043 vtcon_port_populate(struct vtcon_port *port)
1044 {
1045 	struct virtqueue *vq;
1046 	int nbufs, error;
1047 
1048 	vq = port->vtcport_invq;
1049 	error = ENOSPC;
1050 
1051 	for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1052 		error = vtcon_port_create_buf(port);
1053 		if (error)
1054 			break;
1055 	}
1056 
1057 	if (nbufs > 0) {
1058 		virtqueue_notify(vq);
1059 		error = 0;
1060 	}
1061 
1062 	return (error);
1063 }
1064 
1065 static void
1066 vtcon_port_destroy(struct vtcon_port *port)
1067 {
1068 
1069 	port->vtcport_sc = NULL;
1070 	port->vtcport_scport = NULL;
1071 	port->vtcport_invq = NULL;
1072 	port->vtcport_outvq = NULL;
1073 	port->vtcport_id = -1;
1074 	mtx_destroy(&port->vtcport_mtx);
1075 	free(port, M_DEVBUF);
1076 }
1077 
1078 static int
1079 vtcon_port_init_vqs(struct vtcon_port *port)
1080 {
1081 	struct vtcon_softc_port *scport;
1082 	int error;
1083 
1084 	scport = port->vtcport_scport;
1085 
1086 	port->vtcport_invq = scport->vcsp_invq;
1087 	port->vtcport_outvq = scport->vcsp_outvq;
1088 
1089 	/*
1090 	 * Free any data left over from when this virtqueue was in use by a
1091 	 * prior port. We have not yet notified the host that the port is
1092 	 * ready, so assume nothing in the virtqueue can be for us.
1093 	 */
1094 	vtcon_port_drain(port);
1095 
1096 	KASSERT(virtqueue_empty(port->vtcport_invq),
1097 	    ("%s: in virtqueue is not empty", __func__));
1098 	KASSERT(virtqueue_empty(port->vtcport_outvq),
1099 	    ("%s: out virtqueue is not empty", __func__));
1100 
1101 	error = vtcon_port_populate(port);
1102 	if (error)
1103 		return (error);
1104 
1105 	return (0);
1106 }
1107 
1108 static int
1109 vtcon_port_create(struct vtcon_softc *sc, int id)
1110 {
1111 	device_t dev;
1112 	struct vtcon_softc_port *scport;
1113 	struct vtcon_port *port;
1114 	int error;
1115 
1116 	dev = sc->vtcon_dev;
1117 	scport = &sc->vtcon_ports[id];
1118 
1119 	VTCON_ASSERT_VALID_PORTID(sc, id);
1120 	MPASS(scport->vcsp_port == NULL);
1121 
1122 	port = malloc(sizeof(struct vtcon_port), M_DEVBUF, M_NOWAIT | M_ZERO);
1123 	if (port == NULL)
1124 		return (ENOMEM);
1125 
1126 	port->vtcport_sc = sc;
1127 	port->vtcport_scport = scport;
1128 	port->vtcport_id = id;
1129 	mtx_init(&port->vtcport_mtx, "vtcpmtx", NULL, MTX_DEF);
1130 	port->vtcport_tty = tty_alloc_mutex(&vtcon_tty_class, port,
1131 	    &port->vtcport_mtx);
1132 
1133 	error = vtcon_port_init_vqs(port);
1134 	if (error) {
1135 		VTCON_PORT_LOCK(port);
1136 		vtcon_port_teardown(port);
1137 		return (error);
1138 	}
1139 
1140 	VTCON_LOCK(sc);
1141 	VTCON_PORT_LOCK(port);
1142 	scport->vcsp_port = port;
1143 	vtcon_port_enable_intr(port);
1144 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_READY, 1);
1145 	VTCON_PORT_UNLOCK(port);
1146 	VTCON_UNLOCK(sc);
1147 
1148 	tty_makedev(port->vtcport_tty, NULL, "%s%r.%r", VTCON_TTY_PREFIX,
1149 	    device_get_unit(dev), id);
1150 
1151 	return (0);
1152 }
1153 
1154 static void
1155 vtcon_port_dev_alias(struct vtcon_port *port, const char *name, size_t len)
1156 {
1157 	struct vtcon_softc *sc;
1158 	struct cdev *pdev;
1159 	struct tty *tp;
1160 	int i, error;
1161 
1162 	sc = port->vtcport_sc;
1163 	tp = port->vtcport_tty;
1164 
1165 	if (port->vtcport_flags & VTCON_PORT_FLAG_ALIAS)
1166 		return;
1167 
1168 	/* Port name is UTF-8, but we can only handle ASCII. */
1169 	for (i = 0; i < len; i++) {
1170 		if (!isascii(name[i]))
1171 			return;
1172 	}
1173 
1174 	/*
1175 	 * Port name may not conform to the devfs requirements so we cannot use
1176 	 * tty_makealias() because the MAKEDEV_CHECKNAME flag must be specified.
1177 	 */
1178 	error = make_dev_alias_p(MAKEDEV_NOWAIT | MAKEDEV_CHECKNAME, &pdev,
1179 	    tp->t_dev, "%s/%*s", VTCON_TTY_ALIAS_PREFIX, (int)len, name);
1180 	if (error) {
1181 		device_printf(sc->vtcon_dev,
1182 		    "%s: cannot make dev alias (%s/%*s) error %d\n", __func__,
1183 		    VTCON_TTY_ALIAS_PREFIX, (int)len, name, error);
1184 	} else
1185 		port->vtcport_flags |= VTCON_PORT_FLAG_ALIAS;
1186 }
1187 
1188 static void
1189 vtcon_port_drain_bufs(struct virtqueue *vq)
1190 {
1191 	void *buf;
1192 	int last;
1193 
1194 	last = 0;
1195 
1196 	while ((buf = virtqueue_drain(vq, &last)) != NULL)
1197 		free(buf, M_DEVBUF);
1198 }
1199 
1200 static void
1201 vtcon_port_drain(struct vtcon_port *port)
1202 {
1203 
1204 	vtcon_port_drain_bufs(port->vtcport_invq);
1205 }
1206 
1207 static void
1208 vtcon_port_teardown(struct vtcon_port *port)
1209 {
1210 	struct tty *tp;
1211 
1212 	tp = port->vtcport_tty;
1213 
1214 	port->vtcport_flags |= VTCON_PORT_FLAG_GONE;
1215 
1216 	if (tp != NULL) {
1217 		atomic_add_int(&vtcon_pending_free, 1);
1218 		tty_rel_gone(tp);
1219 	} else
1220 		vtcon_port_destroy(port);
1221 }
1222 
1223 static void
1224 vtcon_port_change_size(struct vtcon_port *port, uint16_t cols, uint16_t rows)
1225 {
1226 	struct tty *tp;
1227 	struct winsize sz;
1228 
1229 	tp = port->vtcport_tty;
1230 
1231 	if (tp == NULL)
1232 		return;
1233 
1234 	bzero(&sz, sizeof(struct winsize));
1235 	sz.ws_col = cols;
1236 	sz.ws_row = rows;
1237 
1238 	tty_set_winsize(tp, &sz);
1239 }
1240 
1241 static void
1242 vtcon_port_update_console_size(struct vtcon_softc *sc)
1243 {
1244 	struct vtcon_port *port;
1245 	struct vtcon_softc_port *scport;
1246 	uint16_t cols, rows;
1247 
1248 	vtcon_get_console_size(sc, &cols, &rows);
1249 
1250 	/*
1251 	 * For now, assume the first (only) port is the console. Note
1252 	 * QEMU does not implement this feature yet.
1253 	 */
1254 	scport = &sc->vtcon_ports[0];
1255 
1256 	VTCON_LOCK(sc);
1257 	port = scport->vcsp_port;
1258 
1259 	if (port != NULL) {
1260 		VTCON_PORT_LOCK(port);
1261 		VTCON_UNLOCK(sc);
1262 		vtcon_port_change_size(port, cols, rows);
1263 		VTCON_PORT_UNLOCK(port);
1264 	} else
1265 		VTCON_UNLOCK(sc);
1266 }
1267 
1268 static void
1269 vtcon_port_enable_intr(struct vtcon_port *port)
1270 {
1271 
1272 	/*
1273 	 * NOTE: The out virtqueue is always polled, so its interrupt
1274 	 * kept disabled.
1275 	 */
1276 	virtqueue_enable_intr(port->vtcport_invq);
1277 }
1278 
1279 static void
1280 vtcon_port_disable_intr(struct vtcon_port *port)
1281 {
1282 
1283 	if (port->vtcport_invq != NULL)
1284 		virtqueue_disable_intr(port->vtcport_invq);
1285 	if (port->vtcport_outvq != NULL)
1286 		virtqueue_disable_intr(port->vtcport_outvq);
1287 }
1288 
1289 static void
1290 vtcon_port_in(struct vtcon_port *port)
1291 {
1292 	struct virtqueue *vq;
1293 	struct tty *tp;
1294 	char *buf;
1295 	uint32_t len;
1296 	int i, deq;
1297 
1298 	tp = port->vtcport_tty;
1299 	vq = port->vtcport_invq;
1300 
1301 again:
1302 	deq = 0;
1303 
1304 	while ((buf = virtqueue_dequeue(vq, &len)) != NULL) {
1305 		for (i = 0; i < len; i++) {
1306 #if defined(KDB)
1307 			if (port->vtcport_flags & VTCON_PORT_FLAG_CONSOLE)
1308 				kdb_alt_break(buf[i],
1309 				    &port->vtcport_alt_break_state);
1310 #endif
1311 			ttydisc_rint(tp, buf[i], 0);
1312 		}
1313 		vtcon_port_requeue_buf(port, buf);
1314 		deq++;
1315 	}
1316 	ttydisc_rint_done(tp);
1317 
1318 	if (deq > 0)
1319 		virtqueue_notify(vq);
1320 
1321 	if (virtqueue_enable_intr(vq) != 0)
1322 		goto again;
1323 }
1324 
1325 static void
1326 vtcon_port_intr(void *scportx)
1327 {
1328 	struct vtcon_softc_port *scport;
1329 	struct vtcon_softc *sc;
1330 	struct vtcon_port *port;
1331 
1332 	scport = scportx;
1333 	sc = scport->vcsp_sc;
1334 
1335 	VTCON_LOCK(sc);
1336 	port = scport->vcsp_port;
1337 	if (port == NULL) {
1338 		VTCON_UNLOCK(sc);
1339 		return;
1340 	}
1341 	VTCON_PORT_LOCK(port);
1342 	VTCON_UNLOCK(sc);
1343 	if ((port->vtcport_flags & VTCON_PORT_FLAG_GONE) == 0)
1344 		vtcon_port_in(port);
1345 	VTCON_PORT_UNLOCK(port);
1346 }
1347 
1348 static void
1349 vtcon_port_out(struct vtcon_port *port, void *buf, int bufsize)
1350 {
1351 	struct sglist_seg segs[2];
1352 	struct sglist sg;
1353 	struct virtqueue *vq;
1354 	int error;
1355 
1356 	vq = port->vtcport_outvq;
1357 	KASSERT(virtqueue_empty(vq),
1358 	    ("%s: port %p out virtqueue not emtpy", __func__, port));
1359 
1360 	sglist_init(&sg, 2, segs);
1361 	error = sglist_append(&sg, buf, bufsize);
1362 	KASSERT(error == 0, ("%s: error %d adding buffer to sglist",
1363 	    __func__, error));
1364 
1365 	error = virtqueue_enqueue(vq, buf, &sg, sg.sg_nseg, 0);
1366 	if (error == 0) {
1367 		virtqueue_notify(vq);
1368 		virtqueue_poll(vq, NULL);
1369 	}
1370 }
1371 
1372 static void
1373 vtcon_port_submit_event(struct vtcon_port *port, uint16_t event,
1374     uint16_t value)
1375 {
1376 	struct vtcon_softc *sc;
1377 
1378 	sc = port->vtcport_sc;
1379 
1380 	vtcon_ctrl_send_control(sc, port->vtcport_id, event, value);
1381 }
1382 
1383 static int
1384 vtcon_tty_open(struct tty *tp)
1385 {
1386 	struct vtcon_port *port;
1387 
1388 	port = tty_softc(tp);
1389 
1390 	if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1391 		return (ENXIO);
1392 
1393 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1394 
1395 	return (0);
1396 }
1397 
1398 static void
1399 vtcon_tty_close(struct tty *tp)
1400 {
1401 	struct vtcon_port *port;
1402 
1403 	port = tty_softc(tp);
1404 
1405 	if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1406 		return;
1407 
1408 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1409 }
1410 
1411 static void
1412 vtcon_tty_outwakeup(struct tty *tp)
1413 {
1414 	struct vtcon_port *port;
1415 	char buf[VTCON_BULK_BUFSZ];
1416 	int len;
1417 
1418 	port = tty_softc(tp);
1419 
1420 	if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1421 		return;
1422 
1423 	while ((len = ttydisc_getc(tp, buf, sizeof(buf))) != 0)
1424 		vtcon_port_out(port, buf, len);
1425 }
1426 
1427 static void
1428 vtcon_tty_free(void *xport)
1429 {
1430 	struct vtcon_port *port;
1431 
1432 	port = xport;
1433 
1434 	vtcon_port_destroy(port);
1435 	atomic_subtract_int(&vtcon_pending_free, 1);
1436 }
1437 
1438 static void
1439 vtcon_get_console_size(struct vtcon_softc *sc, uint16_t *cols, uint16_t *rows)
1440 {
1441 	struct virtio_console_config concfg;
1442 
1443 	KASSERT(sc->vtcon_flags & VTCON_FLAG_SIZE,
1444 	    ("%s: size feature not negotiated", __func__));
1445 
1446 	vtcon_read_config(sc, &concfg);
1447 
1448 	*cols = concfg.cols;
1449 	*rows = concfg.rows;
1450 }
1451 
1452 static void
1453 vtcon_enable_interrupts(struct vtcon_softc *sc)
1454 {
1455 	struct vtcon_softc_port *scport;
1456 	struct vtcon_port *port;
1457 	int i;
1458 
1459 	VTCON_LOCK(sc);
1460 
1461 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1462 		virtqueue_enable_intr(sc->vtcon_ctrl_rxvq);
1463 
1464 	for (i = 0; i < sc->vtcon_max_ports; i++) {
1465 		scport = &sc->vtcon_ports[i];
1466 
1467 		port = scport->vcsp_port;
1468 		if (port == NULL)
1469 			continue;
1470 
1471 		VTCON_PORT_LOCK(port);
1472 		vtcon_port_enable_intr(port);
1473 		VTCON_PORT_UNLOCK(port);
1474 	}
1475 
1476 	VTCON_UNLOCK(sc);
1477 }
1478 
1479 static void
1480 vtcon_disable_interrupts(struct vtcon_softc *sc)
1481 {
1482 	struct vtcon_softc_port *scport;
1483 	struct vtcon_port *port;
1484 	int i;
1485 
1486 	VTCON_LOCK_ASSERT(sc);
1487 
1488 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1489 		virtqueue_disable_intr(sc->vtcon_ctrl_rxvq);
1490 
1491 	for (i = 0; i < sc->vtcon_max_ports; i++) {
1492 		scport = &sc->vtcon_ports[i];
1493 
1494 		port = scport->vcsp_port;
1495 		if (port == NULL)
1496 			continue;
1497 
1498 		VTCON_PORT_LOCK(port);
1499 		vtcon_port_disable_intr(port);
1500 		VTCON_PORT_UNLOCK(port);
1501 	}
1502 }
1503