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 VIRTIO_SIMPLE_PNPTABLE(virtio_console, VIRTIO_ID_CONSOLE,
265     "VirtIO Console Adapter");
266 VIRTIO_SIMPLE_PNPINFO(virtio_pci, virtio_console);
267 
268 static int
269 vtcon_modevent(module_t mod, int type, void *unused)
270 {
271 	int error;
272 
273 	switch (type) {
274 	case MOD_LOAD:
275 		error = 0;
276 		break;
277 	case MOD_QUIESCE:
278 		error = 0;
279 		break;
280 	case MOD_UNLOAD:
281 		vtcon_drain_all();
282 		error = 0;
283 		break;
284 	case MOD_SHUTDOWN:
285 		error = 0;
286 		break;
287 	default:
288 		error = EOPNOTSUPP;
289 		break;
290 	}
291 
292 	return (error);
293 }
294 
295 static void
296 vtcon_drain_all(void)
297 {
298 	int first;
299 
300 	for (first = 1; vtcon_pending_free != 0; first = 0) {
301 		if (first != 0) {
302 			printf("virtio_console: Waiting for all detached TTY "
303 			    "devices to have open fds closed.\n");
304 		}
305 		pause("vtcondra", hz);
306 	}
307 }
308 
309 static int
310 vtcon_probe(device_t dev)
311 {
312 	return (VIRTIO_SIMPLE_PROBE(dev, virtio_console));
313 }
314 
315 static int
316 vtcon_attach(device_t dev)
317 {
318 	struct vtcon_softc *sc;
319 	struct virtio_console_config concfg;
320 	int error;
321 
322 	sc = device_get_softc(dev);
323 	sc->vtcon_dev = dev;
324 
325 	mtx_init(&sc->vtcon_mtx, "vtconmtx", NULL, MTX_DEF);
326 	mtx_init(&sc->vtcon_ctrl_tx_mtx, "vtconctrlmtx", NULL, MTX_DEF);
327 
328 	virtio_set_feature_desc(dev, vtcon_feature_desc);
329 	vtcon_setup_features(sc);
330 
331 	vtcon_read_config(sc, &concfg);
332 	vtcon_determine_max_ports(sc, &concfg);
333 
334 	error = vtcon_alloc_scports(sc);
335 	if (error) {
336 		device_printf(dev, "cannot allocate softc port structures\n");
337 		goto fail;
338 	}
339 
340 	error = vtcon_alloc_virtqueues(sc);
341 	if (error) {
342 		device_printf(dev, "cannot allocate virtqueues\n");
343 		goto fail;
344 	}
345 
346 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
347 		TASK_INIT(&sc->vtcon_ctrl_task, 0, vtcon_ctrl_task_cb, sc);
348 		error = vtcon_ctrl_init(sc);
349 		if (error)
350 			goto fail;
351 	} else {
352 		error = vtcon_port_create(sc, 0);
353 		if (error)
354 			goto fail;
355 		if (sc->vtcon_flags & VTCON_FLAG_SIZE)
356 			vtcon_port_update_console_size(sc);
357 	}
358 
359 	error = virtio_setup_intr(dev, INTR_TYPE_TTY);
360 	if (error) {
361 		device_printf(dev, "cannot setup virtqueue interrupts\n");
362 		goto fail;
363 	}
364 
365 	vtcon_enable_interrupts(sc);
366 
367 	vtcon_ctrl_send_control(sc, VIRTIO_CONSOLE_BAD_ID,
368 	    VIRTIO_CONSOLE_DEVICE_READY, 1);
369 
370 fail:
371 	if (error)
372 		vtcon_detach(dev);
373 
374 	return (error);
375 }
376 
377 static int
378 vtcon_detach(device_t dev)
379 {
380 	struct vtcon_softc *sc;
381 
382 	sc = device_get_softc(dev);
383 
384 	VTCON_LOCK(sc);
385 	sc->vtcon_flags |= VTCON_FLAG_DETACHED;
386 	if (device_is_attached(dev))
387 		vtcon_stop(sc);
388 	VTCON_UNLOCK(sc);
389 
390 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
391 		taskqueue_drain(taskqueue_thread, &sc->vtcon_ctrl_task);
392 		vtcon_ctrl_deinit(sc);
393 	}
394 
395 	vtcon_destroy_ports(sc);
396 	mtx_destroy(&sc->vtcon_mtx);
397 	mtx_destroy(&sc->vtcon_ctrl_tx_mtx);
398 
399 	return (0);
400 }
401 
402 static int
403 vtcon_config_change(device_t dev)
404 {
405 	struct vtcon_softc *sc;
406 
407 	sc = device_get_softc(dev);
408 
409 	/*
410 	 * When the multiport feature is negotiated, all configuration
411 	 * changes are done through control virtqueue events.
412 	 */
413 	if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0) {
414 		if (sc->vtcon_flags & VTCON_FLAG_SIZE)
415 			vtcon_port_update_console_size(sc);
416 	}
417 
418 	return (0);
419 }
420 
421 static void
422 vtcon_negotiate_features(struct vtcon_softc *sc)
423 {
424 	device_t dev;
425 	uint64_t features;
426 
427 	dev = sc->vtcon_dev;
428 	features = VTCON_FEATURES;
429 
430 	sc->vtcon_features = virtio_negotiate_features(dev, features);
431 }
432 
433 static void
434 vtcon_setup_features(struct vtcon_softc *sc)
435 {
436 	device_t dev;
437 
438 	dev = sc->vtcon_dev;
439 
440 	vtcon_negotiate_features(sc);
441 
442 	if (virtio_with_feature(dev, VIRTIO_CONSOLE_F_SIZE))
443 		sc->vtcon_flags |= VTCON_FLAG_SIZE;
444 	if (virtio_with_feature(dev, VIRTIO_CONSOLE_F_MULTIPORT))
445 		sc->vtcon_flags |= VTCON_FLAG_MULTIPORT;
446 }
447 
448 #define VTCON_GET_CONFIG(_dev, _feature, _field, _cfg)			\
449 	if (virtio_with_feature(_dev, _feature)) {			\
450 		virtio_read_device_config(_dev,				\
451 		    offsetof(struct virtio_console_config, _field),	\
452 		    &(_cfg)->_field, sizeof((_cfg)->_field));		\
453 	}
454 
455 static void
456 vtcon_read_config(struct vtcon_softc *sc, struct virtio_console_config *concfg)
457 {
458 	device_t dev;
459 
460 	dev = sc->vtcon_dev;
461 
462 	bzero(concfg, sizeof(struct virtio_console_config));
463 
464 	VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_SIZE, cols, concfg);
465 	VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_SIZE, rows, concfg);
466 	VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_MULTIPORT, max_nr_ports, concfg);
467 }
468 
469 #undef VTCON_GET_CONFIG
470 
471 static int
472 vtcon_alloc_scports(struct vtcon_softc *sc)
473 {
474 	struct vtcon_softc_port *scport;
475 	int max, i;
476 
477 	max = sc->vtcon_max_ports;
478 
479 	sc->vtcon_ports = malloc(sizeof(struct vtcon_softc_port) * max,
480 	    M_DEVBUF, M_NOWAIT | M_ZERO);
481 	if (sc->vtcon_ports == NULL)
482 		return (ENOMEM);
483 
484 	for (i = 0; i < max; i++) {
485 		scport = &sc->vtcon_ports[i];
486 		scport->vcsp_sc = sc;
487 	}
488 
489 	return (0);
490 }
491 
492 static int
493 vtcon_alloc_virtqueues(struct vtcon_softc *sc)
494 {
495 	device_t dev;
496 	struct vq_alloc_info *info;
497 	struct vtcon_softc_port *scport;
498 	int i, idx, portidx, nvqs, error;
499 
500 	dev = sc->vtcon_dev;
501 
502 	nvqs = sc->vtcon_max_ports * 2;
503 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
504 		nvqs += 2;
505 
506 	info = malloc(sizeof(struct vq_alloc_info) * nvqs, M_TEMP, M_NOWAIT);
507 	if (info == NULL)
508 		return (ENOMEM);
509 
510 	for (i = 0, idx = 0, portidx = 0; i < nvqs / 2; i++, idx += 2) {
511 
512 		if (i == 1) {
513 			/* The control virtqueues are after the first port. */
514 			VQ_ALLOC_INFO_INIT(&info[idx], 0,
515 			    vtcon_ctrl_event_intr, sc, &sc->vtcon_ctrl_rxvq,
516 			    "%s-control rx", device_get_nameunit(dev));
517 			VQ_ALLOC_INFO_INIT(&info[idx+1], 0,
518 			    NULL, sc, &sc->vtcon_ctrl_txvq,
519 			    "%s-control tx", device_get_nameunit(dev));
520 			continue;
521 		}
522 
523 		scport = &sc->vtcon_ports[portidx];
524 
525 		VQ_ALLOC_INFO_INIT(&info[idx], 0, vtcon_port_intr,
526 		    scport, &scport->vcsp_invq, "%s-port%d in",
527 		    device_get_nameunit(dev), i);
528 		VQ_ALLOC_INFO_INIT(&info[idx+1], 0, NULL,
529 		    NULL, &scport->vcsp_outvq, "%s-port%d out",
530 		    device_get_nameunit(dev), i);
531 
532 		portidx++;
533 	}
534 
535 	error = virtio_alloc_virtqueues(dev, 0, nvqs, info);
536 	free(info, M_TEMP);
537 
538 	return (error);
539 }
540 
541 static void
542 vtcon_determine_max_ports(struct vtcon_softc *sc,
543     struct virtio_console_config *concfg)
544 {
545 
546 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
547 		sc->vtcon_max_ports =
548 		    min(concfg->max_nr_ports, VTCON_MAX_PORTS);
549 		if (sc->vtcon_max_ports == 0)
550 			sc->vtcon_max_ports = 1;
551 	} else
552 		sc->vtcon_max_ports = 1;
553 }
554 
555 static void
556 vtcon_destroy_ports(struct vtcon_softc *sc)
557 {
558 	struct vtcon_softc_port *scport;
559 	struct vtcon_port *port;
560 	struct virtqueue *vq;
561 	int i;
562 
563 	if (sc->vtcon_ports == NULL)
564 		return;
565 
566 	VTCON_LOCK(sc);
567 	for (i = 0; i < sc->vtcon_max_ports; i++) {
568 		scport = &sc->vtcon_ports[i];
569 
570 		port = scport->vcsp_port;
571 		if (port != NULL) {
572 			scport->vcsp_port = NULL;
573 			VTCON_PORT_LOCK(port);
574 			VTCON_UNLOCK(sc);
575 			vtcon_port_teardown(port);
576 			VTCON_LOCK(sc);
577 		}
578 
579 		vq = scport->vcsp_invq;
580 		if (vq != NULL)
581 			vtcon_port_drain_bufs(vq);
582 	}
583 	VTCON_UNLOCK(sc);
584 
585 	free(sc->vtcon_ports, M_DEVBUF);
586 	sc->vtcon_ports = NULL;
587 }
588 
589 static void
590 vtcon_stop(struct vtcon_softc *sc)
591 {
592 
593 	vtcon_disable_interrupts(sc);
594 	virtio_stop(sc->vtcon_dev);
595 }
596 
597 static int
598 vtcon_ctrl_event_enqueue(struct vtcon_softc *sc,
599     struct virtio_console_control *control)
600 {
601 	struct sglist_seg segs[2];
602 	struct sglist sg;
603 	struct virtqueue *vq;
604 	int error;
605 
606 	vq = sc->vtcon_ctrl_rxvq;
607 
608 	sglist_init(&sg, 2, segs);
609 	error = sglist_append(&sg, control, VTCON_CTRL_BUFSZ);
610 	KASSERT(error == 0, ("%s: error %d adding control to sglist",
611 	    __func__, error));
612 
613 	return (virtqueue_enqueue(vq, control, &sg, 0, sg.sg_nseg));
614 }
615 
616 static int
617 vtcon_ctrl_event_create(struct vtcon_softc *sc)
618 {
619 	struct virtio_console_control *control;
620 	int error;
621 
622 	control = malloc(VTCON_CTRL_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
623 	if (control == NULL)
624 		return (ENOMEM);
625 
626 	error = vtcon_ctrl_event_enqueue(sc, control);
627 	if (error)
628 		free(control, M_DEVBUF);
629 
630 	return (error);
631 }
632 
633 static void
634 vtcon_ctrl_event_requeue(struct vtcon_softc *sc,
635     struct virtio_console_control *control)
636 {
637 	int error;
638 
639 	bzero(control, VTCON_CTRL_BUFSZ);
640 
641 	error = vtcon_ctrl_event_enqueue(sc, control);
642 	KASSERT(error == 0,
643 	    ("%s: cannot requeue control buffer %d", __func__, error));
644 }
645 
646 static int
647 vtcon_ctrl_event_populate(struct vtcon_softc *sc)
648 {
649 	struct virtqueue *vq;
650 	int nbufs, error;
651 
652 	vq = sc->vtcon_ctrl_rxvq;
653 	error = ENOSPC;
654 
655 	for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
656 		error = vtcon_ctrl_event_create(sc);
657 		if (error)
658 			break;
659 	}
660 
661 	if (nbufs > 0) {
662 		virtqueue_notify(vq);
663 		error = 0;
664 	}
665 
666 	return (error);
667 }
668 
669 static void
670 vtcon_ctrl_event_drain(struct vtcon_softc *sc)
671 {
672 	struct virtio_console_control *control;
673 	struct virtqueue *vq;
674 	int last;
675 
676 	vq = sc->vtcon_ctrl_rxvq;
677 	last = 0;
678 
679 	if (vq == NULL)
680 		return;
681 
682 	VTCON_LOCK(sc);
683 	while ((control = virtqueue_drain(vq, &last)) != NULL)
684 		free(control, M_DEVBUF);
685 	VTCON_UNLOCK(sc);
686 }
687 
688 static int
689 vtcon_ctrl_init(struct vtcon_softc *sc)
690 {
691 	int error;
692 
693 	error = vtcon_ctrl_event_populate(sc);
694 
695 	return (error);
696 }
697 
698 static void
699 vtcon_ctrl_deinit(struct vtcon_softc *sc)
700 {
701 
702 	vtcon_ctrl_event_drain(sc);
703 }
704 
705 static void
706 vtcon_ctrl_port_add_event(struct vtcon_softc *sc, int id)
707 {
708 	device_t dev;
709 	int error;
710 
711 	dev = sc->vtcon_dev;
712 
713 	/* This single thread only way for ports to be created. */
714 	if (sc->vtcon_ports[id].vcsp_port != NULL) {
715 		device_printf(dev, "%s: adding port %d, but already exists\n",
716 		    __func__, id);
717 		return;
718 	}
719 
720 	error = vtcon_port_create(sc, id);
721 	if (error) {
722 		device_printf(dev, "%s: cannot create port %d: %d\n",
723 		    __func__, id, error);
724 		vtcon_ctrl_send_control(sc, id, VIRTIO_CONSOLE_PORT_READY, 0);
725 		return;
726 	}
727 }
728 
729 static void
730 vtcon_ctrl_port_remove_event(struct vtcon_softc *sc, int id)
731 {
732 	device_t dev;
733 	struct vtcon_softc_port *scport;
734 	struct vtcon_port *port;
735 
736 	dev = sc->vtcon_dev;
737 	scport = &sc->vtcon_ports[id];
738 
739 	VTCON_LOCK(sc);
740 	port = scport->vcsp_port;
741 	if (port == NULL) {
742 		VTCON_UNLOCK(sc);
743 		device_printf(dev, "%s: remove port %d, but does not exist\n",
744 		    __func__, id);
745 		return;
746 	}
747 
748 	scport->vcsp_port = NULL;
749 	VTCON_PORT_LOCK(port);
750 	VTCON_UNLOCK(sc);
751 	vtcon_port_teardown(port);
752 }
753 
754 static void
755 vtcon_ctrl_port_console_event(struct vtcon_softc *sc, int id)
756 {
757 	device_t dev;
758 	struct vtcon_softc_port *scport;
759 	struct vtcon_port *port;
760 
761 	dev = sc->vtcon_dev;
762 	scport = &sc->vtcon_ports[id];
763 
764 	VTCON_LOCK(sc);
765 	port = scport->vcsp_port;
766 	if (port == NULL) {
767 		VTCON_UNLOCK(sc);
768 		device_printf(dev, "%s: console port %d, but does not exist\n",
769 		    __func__, id);
770 		return;
771 	}
772 
773 	VTCON_PORT_LOCK(port);
774 	VTCON_UNLOCK(sc);
775 	port->vtcport_flags |= VTCON_PORT_FLAG_CONSOLE;
776 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
777 	VTCON_PORT_UNLOCK(port);
778 }
779 
780 static void
781 vtcon_ctrl_port_open_event(struct vtcon_softc *sc, int id)
782 {
783 	device_t dev;
784 	struct vtcon_softc_port *scport;
785 	struct vtcon_port *port;
786 
787 	dev = sc->vtcon_dev;
788 	scport = &sc->vtcon_ports[id];
789 
790 	VTCON_LOCK(sc);
791 	port = scport->vcsp_port;
792 	if (port == NULL) {
793 		VTCON_UNLOCK(sc);
794 		device_printf(dev, "%s: open port %d, but does not exist\n",
795 		    __func__, id);
796 		return;
797 	}
798 
799 	VTCON_PORT_LOCK(port);
800 	VTCON_UNLOCK(sc);
801 	vtcon_port_enable_intr(port);
802 	VTCON_PORT_UNLOCK(port);
803 }
804 
805 static void
806 vtcon_ctrl_port_name_event(struct vtcon_softc *sc, int id, const char *name,
807     size_t len)
808 {
809 	device_t dev;
810 	struct vtcon_softc_port *scport;
811 	struct vtcon_port *port;
812 
813 	dev = sc->vtcon_dev;
814 	scport = &sc->vtcon_ports[id];
815 
816 	/*
817 	 * The VirtIO specification says the NUL terminator is not included in
818 	 * the length, but QEMU includes it. Adjust the length if needed.
819 	 */
820 	if (name == NULL || len == 0)
821 		return;
822 	if (name[len - 1] == '\0') {
823 		len--;
824 		if (len == 0)
825 			return;
826 	}
827 
828 	VTCON_LOCK(sc);
829 	port = scport->vcsp_port;
830 	if (port == NULL) {
831 		VTCON_UNLOCK(sc);
832 		device_printf(dev, "%s: name port %d, but does not exist\n",
833 		    __func__, id);
834 		return;
835 	}
836 
837 	VTCON_PORT_LOCK(port);
838 	VTCON_UNLOCK(sc);
839 	vtcon_port_dev_alias(port, name, len);
840 	VTCON_PORT_UNLOCK(port);
841 }
842 
843 static void
844 vtcon_ctrl_process_event(struct vtcon_softc *sc,
845     struct virtio_console_control *control, void *data, size_t data_len)
846 {
847 	device_t dev;
848 	int id;
849 
850 	dev = sc->vtcon_dev;
851 	id = control->id;
852 
853 	if (id < 0 || id >= sc->vtcon_max_ports) {
854 		device_printf(dev, "%s: invalid port ID %d\n", __func__, id);
855 		return;
856 	}
857 
858 	switch (control->event) {
859 	case VIRTIO_CONSOLE_PORT_ADD:
860 		vtcon_ctrl_port_add_event(sc, id);
861 		break;
862 
863 	case VIRTIO_CONSOLE_PORT_REMOVE:
864 		vtcon_ctrl_port_remove_event(sc, id);
865 		break;
866 
867 	case VIRTIO_CONSOLE_CONSOLE_PORT:
868 		vtcon_ctrl_port_console_event(sc, id);
869 		break;
870 
871 	case VIRTIO_CONSOLE_RESIZE:
872 		break;
873 
874 	case VIRTIO_CONSOLE_PORT_OPEN:
875 		vtcon_ctrl_port_open_event(sc, id);
876 		break;
877 
878 	case VIRTIO_CONSOLE_PORT_NAME:
879 		vtcon_ctrl_port_name_event(sc, id, (const char *)data, data_len);
880 		break;
881 	}
882 }
883 
884 static void
885 vtcon_ctrl_task_cb(void *xsc, int pending)
886 {
887 	struct vtcon_softc *sc;
888 	struct virtqueue *vq;
889 	struct virtio_console_control *control;
890 	void *data;
891 	size_t data_len;
892 	int detached;
893 	uint32_t len;
894 
895 	sc = xsc;
896 	vq = sc->vtcon_ctrl_rxvq;
897 
898 	VTCON_LOCK(sc);
899 
900 	while ((detached = (sc->vtcon_flags & VTCON_FLAG_DETACHED)) == 0) {
901 		control = virtqueue_dequeue(vq, &len);
902 		if (control == NULL)
903 			break;
904 
905 		if (len > sizeof(struct virtio_console_control)) {
906 			data = (void *) &control[1];
907 			data_len = len - sizeof(struct virtio_console_control);
908 		} else {
909 			data = NULL;
910 			data_len = 0;
911 		}
912 
913 		VTCON_UNLOCK(sc);
914 		vtcon_ctrl_process_event(sc, control, data, data_len);
915 		VTCON_LOCK(sc);
916 		vtcon_ctrl_event_requeue(sc, control);
917 	}
918 
919 	if (!detached) {
920 		virtqueue_notify(vq);
921 		if (virtqueue_enable_intr(vq) != 0)
922 			taskqueue_enqueue(taskqueue_thread,
923 			    &sc->vtcon_ctrl_task);
924 	}
925 
926 	VTCON_UNLOCK(sc);
927 }
928 
929 static void
930 vtcon_ctrl_event_intr(void *xsc)
931 {
932 	struct vtcon_softc *sc;
933 
934 	sc = xsc;
935 
936 	/*
937 	 * Only some events require us to potentially block, but it
938 	 * easier to just defer all event handling to the taskqueue.
939 	 */
940 	taskqueue_enqueue(taskqueue_thread, &sc->vtcon_ctrl_task);
941 }
942 
943 static void
944 vtcon_ctrl_poll(struct vtcon_softc *sc,
945     struct virtio_console_control *control)
946 {
947 	struct sglist_seg segs[2];
948 	struct sglist sg;
949 	struct virtqueue *vq;
950 	int error;
951 
952 	vq = sc->vtcon_ctrl_txvq;
953 
954 	sglist_init(&sg, 2, segs);
955 	error = sglist_append(&sg, control,
956 	    sizeof(struct virtio_console_control));
957 	KASSERT(error == 0, ("%s: error %d adding control to sglist",
958 	    __func__, error));
959 
960 	/*
961 	 * We cannot use the softc lock to serialize access to this
962 	 * virtqueue since this is called from the tty layer with the
963 	 * port lock held. Acquiring the softc would violate our lock
964 	 * ordering.
965 	 */
966 	VTCON_CTRL_TX_LOCK(sc);
967 	KASSERT(virtqueue_empty(vq),
968 	    ("%s: virtqueue is not emtpy", __func__));
969 	error = virtqueue_enqueue(vq, control, &sg, sg.sg_nseg, 0);
970 	if (error == 0) {
971 		virtqueue_notify(vq);
972 		virtqueue_poll(vq, NULL);
973 	}
974 	VTCON_CTRL_TX_UNLOCK(sc);
975 }
976 
977 static void
978 vtcon_ctrl_send_control(struct vtcon_softc *sc, uint32_t portid,
979     uint16_t event, uint16_t value)
980 {
981 	struct virtio_console_control control;
982 
983 	if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0)
984 		return;
985 
986 	control.id = portid;
987 	control.event = event;
988 	control.value = value;
989 
990 	vtcon_ctrl_poll(sc, &control);
991 }
992 
993 static int
994 vtcon_port_enqueue_buf(struct vtcon_port *port, void *buf, size_t len)
995 {
996 	struct sglist_seg segs[2];
997 	struct sglist sg;
998 	struct virtqueue *vq;
999 	int error;
1000 
1001 	vq = port->vtcport_invq;
1002 
1003 	sglist_init(&sg, 2, segs);
1004 	error = sglist_append(&sg, buf, len);
1005 	KASSERT(error == 0,
1006 	    ("%s: error %d adding buffer to sglist", __func__, error));
1007 
1008 	error = virtqueue_enqueue(vq, buf, &sg, 0, sg.sg_nseg);
1009 
1010 	return (error);
1011 }
1012 
1013 static int
1014 vtcon_port_create_buf(struct vtcon_port *port)
1015 {
1016 	void *buf;
1017 	int error;
1018 
1019 	buf = malloc(VTCON_BULK_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
1020 	if (buf == NULL)
1021 		return (ENOMEM);
1022 
1023 	error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1024 	if (error)
1025 		free(buf, M_DEVBUF);
1026 
1027 	return (error);
1028 }
1029 
1030 static void
1031 vtcon_port_requeue_buf(struct vtcon_port *port, void *buf)
1032 {
1033 	int error;
1034 
1035 	error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1036 	KASSERT(error == 0,
1037 	    ("%s: cannot requeue input buffer %d", __func__, error));
1038 }
1039 
1040 static int
1041 vtcon_port_populate(struct vtcon_port *port)
1042 {
1043 	struct virtqueue *vq;
1044 	int nbufs, error;
1045 
1046 	vq = port->vtcport_invq;
1047 	error = ENOSPC;
1048 
1049 	for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1050 		error = vtcon_port_create_buf(port);
1051 		if (error)
1052 			break;
1053 	}
1054 
1055 	if (nbufs > 0) {
1056 		virtqueue_notify(vq);
1057 		error = 0;
1058 	}
1059 
1060 	return (error);
1061 }
1062 
1063 static void
1064 vtcon_port_destroy(struct vtcon_port *port)
1065 {
1066 
1067 	port->vtcport_sc = NULL;
1068 	port->vtcport_scport = NULL;
1069 	port->vtcport_invq = NULL;
1070 	port->vtcport_outvq = NULL;
1071 	port->vtcport_id = -1;
1072 	mtx_destroy(&port->vtcport_mtx);
1073 	free(port, M_DEVBUF);
1074 }
1075 
1076 static int
1077 vtcon_port_init_vqs(struct vtcon_port *port)
1078 {
1079 	struct vtcon_softc_port *scport;
1080 	int error;
1081 
1082 	scport = port->vtcport_scport;
1083 
1084 	port->vtcport_invq = scport->vcsp_invq;
1085 	port->vtcport_outvq = scport->vcsp_outvq;
1086 
1087 	/*
1088 	 * Free any data left over from when this virtqueue was in use by a
1089 	 * prior port. We have not yet notified the host that the port is
1090 	 * ready, so assume nothing in the virtqueue can be for us.
1091 	 */
1092 	vtcon_port_drain(port);
1093 
1094 	KASSERT(virtqueue_empty(port->vtcport_invq),
1095 	    ("%s: in virtqueue is not empty", __func__));
1096 	KASSERT(virtqueue_empty(port->vtcport_outvq),
1097 	    ("%s: out virtqueue is not empty", __func__));
1098 
1099 	error = vtcon_port_populate(port);
1100 	if (error)
1101 		return (error);
1102 
1103 	return (0);
1104 }
1105 
1106 static int
1107 vtcon_port_create(struct vtcon_softc *sc, int id)
1108 {
1109 	device_t dev;
1110 	struct vtcon_softc_port *scport;
1111 	struct vtcon_port *port;
1112 	int error;
1113 
1114 	dev = sc->vtcon_dev;
1115 	scport = &sc->vtcon_ports[id];
1116 
1117 	VTCON_ASSERT_VALID_PORTID(sc, id);
1118 	MPASS(scport->vcsp_port == NULL);
1119 
1120 	port = malloc(sizeof(struct vtcon_port), M_DEVBUF, M_NOWAIT | M_ZERO);
1121 	if (port == NULL)
1122 		return (ENOMEM);
1123 
1124 	port->vtcport_sc = sc;
1125 	port->vtcport_scport = scport;
1126 	port->vtcport_id = id;
1127 	mtx_init(&port->vtcport_mtx, "vtcpmtx", NULL, MTX_DEF);
1128 	port->vtcport_tty = tty_alloc_mutex(&vtcon_tty_class, port,
1129 	    &port->vtcport_mtx);
1130 
1131 	error = vtcon_port_init_vqs(port);
1132 	if (error) {
1133 		VTCON_PORT_LOCK(port);
1134 		vtcon_port_teardown(port);
1135 		return (error);
1136 	}
1137 
1138 	VTCON_LOCK(sc);
1139 	VTCON_PORT_LOCK(port);
1140 	scport->vcsp_port = port;
1141 	vtcon_port_enable_intr(port);
1142 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_READY, 1);
1143 	VTCON_PORT_UNLOCK(port);
1144 	VTCON_UNLOCK(sc);
1145 
1146 	tty_makedev(port->vtcport_tty, NULL, "%s%r.%r", VTCON_TTY_PREFIX,
1147 	    device_get_unit(dev), id);
1148 
1149 	return (0);
1150 }
1151 
1152 static void
1153 vtcon_port_dev_alias(struct vtcon_port *port, const char *name, size_t len)
1154 {
1155 	struct vtcon_softc *sc;
1156 	struct cdev *pdev;
1157 	struct tty *tp;
1158 	int i, error;
1159 
1160 	sc = port->vtcport_sc;
1161 	tp = port->vtcport_tty;
1162 
1163 	if (port->vtcport_flags & VTCON_PORT_FLAG_ALIAS)
1164 		return;
1165 
1166 	/* Port name is UTF-8, but we can only handle ASCII. */
1167 	for (i = 0; i < len; i++) {
1168 		if (!isascii(name[i]))
1169 			return;
1170 	}
1171 
1172 	/*
1173 	 * Port name may not conform to the devfs requirements so we cannot use
1174 	 * tty_makealias() because the MAKEDEV_CHECKNAME flag must be specified.
1175 	 */
1176 	error = make_dev_alias_p(MAKEDEV_NOWAIT | MAKEDEV_CHECKNAME, &pdev,
1177 	    tp->t_dev, "%s/%*s", VTCON_TTY_ALIAS_PREFIX, (int)len, name);
1178 	if (error) {
1179 		device_printf(sc->vtcon_dev,
1180 		    "%s: cannot make dev alias (%s/%*s) error %d\n", __func__,
1181 		    VTCON_TTY_ALIAS_PREFIX, (int)len, name, error);
1182 	} else
1183 		port->vtcport_flags |= VTCON_PORT_FLAG_ALIAS;
1184 }
1185 
1186 static void
1187 vtcon_port_drain_bufs(struct virtqueue *vq)
1188 {
1189 	void *buf;
1190 	int last;
1191 
1192 	last = 0;
1193 
1194 	while ((buf = virtqueue_drain(vq, &last)) != NULL)
1195 		free(buf, M_DEVBUF);
1196 }
1197 
1198 static void
1199 vtcon_port_drain(struct vtcon_port *port)
1200 {
1201 
1202 	vtcon_port_drain_bufs(port->vtcport_invq);
1203 }
1204 
1205 static void
1206 vtcon_port_teardown(struct vtcon_port *port)
1207 {
1208 	struct tty *tp;
1209 
1210 	tp = port->vtcport_tty;
1211 
1212 	port->vtcport_flags |= VTCON_PORT_FLAG_GONE;
1213 
1214 	if (tp != NULL) {
1215 		atomic_add_int(&vtcon_pending_free, 1);
1216 		tty_rel_gone(tp);
1217 	} else
1218 		vtcon_port_destroy(port);
1219 }
1220 
1221 static void
1222 vtcon_port_change_size(struct vtcon_port *port, uint16_t cols, uint16_t rows)
1223 {
1224 	struct tty *tp;
1225 	struct winsize sz;
1226 
1227 	tp = port->vtcport_tty;
1228 
1229 	if (tp == NULL)
1230 		return;
1231 
1232 	bzero(&sz, sizeof(struct winsize));
1233 	sz.ws_col = cols;
1234 	sz.ws_row = rows;
1235 
1236 	tty_set_winsize(tp, &sz);
1237 }
1238 
1239 static void
1240 vtcon_port_update_console_size(struct vtcon_softc *sc)
1241 {
1242 	struct vtcon_port *port;
1243 	struct vtcon_softc_port *scport;
1244 	uint16_t cols, rows;
1245 
1246 	vtcon_get_console_size(sc, &cols, &rows);
1247 
1248 	/*
1249 	 * For now, assume the first (only) port is the console. Note
1250 	 * QEMU does not implement this feature yet.
1251 	 */
1252 	scport = &sc->vtcon_ports[0];
1253 
1254 	VTCON_LOCK(sc);
1255 	port = scport->vcsp_port;
1256 
1257 	if (port != NULL) {
1258 		VTCON_PORT_LOCK(port);
1259 		VTCON_UNLOCK(sc);
1260 		vtcon_port_change_size(port, cols, rows);
1261 		VTCON_PORT_UNLOCK(port);
1262 	} else
1263 		VTCON_UNLOCK(sc);
1264 }
1265 
1266 static void
1267 vtcon_port_enable_intr(struct vtcon_port *port)
1268 {
1269 
1270 	/*
1271 	 * NOTE: The out virtqueue is always polled, so its interrupt
1272 	 * kept disabled.
1273 	 */
1274 	virtqueue_enable_intr(port->vtcport_invq);
1275 }
1276 
1277 static void
1278 vtcon_port_disable_intr(struct vtcon_port *port)
1279 {
1280 
1281 	if (port->vtcport_invq != NULL)
1282 		virtqueue_disable_intr(port->vtcport_invq);
1283 	if (port->vtcport_outvq != NULL)
1284 		virtqueue_disable_intr(port->vtcport_outvq);
1285 }
1286 
1287 static void
1288 vtcon_port_in(struct vtcon_port *port)
1289 {
1290 	struct virtqueue *vq;
1291 	struct tty *tp;
1292 	char *buf;
1293 	uint32_t len;
1294 	int i, deq;
1295 
1296 	tp = port->vtcport_tty;
1297 	vq = port->vtcport_invq;
1298 
1299 again:
1300 	deq = 0;
1301 
1302 	while ((buf = virtqueue_dequeue(vq, &len)) != NULL) {
1303 		for (i = 0; i < len; i++) {
1304 #if defined(KDB)
1305 			if (port->vtcport_flags & VTCON_PORT_FLAG_CONSOLE)
1306 				kdb_alt_break(buf[i],
1307 				    &port->vtcport_alt_break_state);
1308 #endif
1309 			ttydisc_rint(tp, buf[i], 0);
1310 		}
1311 		vtcon_port_requeue_buf(port, buf);
1312 		deq++;
1313 	}
1314 	ttydisc_rint_done(tp);
1315 
1316 	if (deq > 0)
1317 		virtqueue_notify(vq);
1318 
1319 	if (virtqueue_enable_intr(vq) != 0)
1320 		goto again;
1321 }
1322 
1323 static void
1324 vtcon_port_intr(void *scportx)
1325 {
1326 	struct vtcon_softc_port *scport;
1327 	struct vtcon_softc *sc;
1328 	struct vtcon_port *port;
1329 
1330 	scport = scportx;
1331 	sc = scport->vcsp_sc;
1332 
1333 	VTCON_LOCK(sc);
1334 	port = scport->vcsp_port;
1335 	if (port == NULL) {
1336 		VTCON_UNLOCK(sc);
1337 		return;
1338 	}
1339 	VTCON_PORT_LOCK(port);
1340 	VTCON_UNLOCK(sc);
1341 	if ((port->vtcport_flags & VTCON_PORT_FLAG_GONE) == 0)
1342 		vtcon_port_in(port);
1343 	VTCON_PORT_UNLOCK(port);
1344 }
1345 
1346 static void
1347 vtcon_port_out(struct vtcon_port *port, void *buf, int bufsize)
1348 {
1349 	struct sglist_seg segs[2];
1350 	struct sglist sg;
1351 	struct virtqueue *vq;
1352 	int error;
1353 
1354 	vq = port->vtcport_outvq;
1355 	KASSERT(virtqueue_empty(vq),
1356 	    ("%s: port %p out virtqueue not emtpy", __func__, port));
1357 
1358 	sglist_init(&sg, 2, segs);
1359 	error = sglist_append(&sg, buf, bufsize);
1360 	KASSERT(error == 0, ("%s: error %d adding buffer to sglist",
1361 	    __func__, error));
1362 
1363 	error = virtqueue_enqueue(vq, buf, &sg, sg.sg_nseg, 0);
1364 	if (error == 0) {
1365 		virtqueue_notify(vq);
1366 		virtqueue_poll(vq, NULL);
1367 	}
1368 }
1369 
1370 static void
1371 vtcon_port_submit_event(struct vtcon_port *port, uint16_t event,
1372     uint16_t value)
1373 {
1374 	struct vtcon_softc *sc;
1375 
1376 	sc = port->vtcport_sc;
1377 
1378 	vtcon_ctrl_send_control(sc, port->vtcport_id, event, value);
1379 }
1380 
1381 static int
1382 vtcon_tty_open(struct tty *tp)
1383 {
1384 	struct vtcon_port *port;
1385 
1386 	port = tty_softc(tp);
1387 
1388 	if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1389 		return (ENXIO);
1390 
1391 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1392 
1393 	return (0);
1394 }
1395 
1396 static void
1397 vtcon_tty_close(struct tty *tp)
1398 {
1399 	struct vtcon_port *port;
1400 
1401 	port = tty_softc(tp);
1402 
1403 	if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1404 		return;
1405 
1406 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1407 }
1408 
1409 static void
1410 vtcon_tty_outwakeup(struct tty *tp)
1411 {
1412 	struct vtcon_port *port;
1413 	char buf[VTCON_BULK_BUFSZ];
1414 	int len;
1415 
1416 	port = tty_softc(tp);
1417 
1418 	if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1419 		return;
1420 
1421 	while ((len = ttydisc_getc(tp, buf, sizeof(buf))) != 0)
1422 		vtcon_port_out(port, buf, len);
1423 }
1424 
1425 static void
1426 vtcon_tty_free(void *xport)
1427 {
1428 	struct vtcon_port *port;
1429 
1430 	port = xport;
1431 
1432 	vtcon_port_destroy(port);
1433 	atomic_subtract_int(&vtcon_pending_free, 1);
1434 }
1435 
1436 static void
1437 vtcon_get_console_size(struct vtcon_softc *sc, uint16_t *cols, uint16_t *rows)
1438 {
1439 	struct virtio_console_config concfg;
1440 
1441 	KASSERT(sc->vtcon_flags & VTCON_FLAG_SIZE,
1442 	    ("%s: size feature not negotiated", __func__));
1443 
1444 	vtcon_read_config(sc, &concfg);
1445 
1446 	*cols = concfg.cols;
1447 	*rows = concfg.rows;
1448 }
1449 
1450 static void
1451 vtcon_enable_interrupts(struct vtcon_softc *sc)
1452 {
1453 	struct vtcon_softc_port *scport;
1454 	struct vtcon_port *port;
1455 	int i;
1456 
1457 	VTCON_LOCK(sc);
1458 
1459 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1460 		virtqueue_enable_intr(sc->vtcon_ctrl_rxvq);
1461 
1462 	for (i = 0; i < sc->vtcon_max_ports; i++) {
1463 		scport = &sc->vtcon_ports[i];
1464 
1465 		port = scport->vcsp_port;
1466 		if (port == NULL)
1467 			continue;
1468 
1469 		VTCON_PORT_LOCK(port);
1470 		vtcon_port_enable_intr(port);
1471 		VTCON_PORT_UNLOCK(port);
1472 	}
1473 
1474 	VTCON_UNLOCK(sc);
1475 }
1476 
1477 static void
1478 vtcon_disable_interrupts(struct vtcon_softc *sc)
1479 {
1480 	struct vtcon_softc_port *scport;
1481 	struct vtcon_port *port;
1482 	int i;
1483 
1484 	VTCON_LOCK_ASSERT(sc);
1485 
1486 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1487 		virtqueue_disable_intr(sc->vtcon_ctrl_rxvq);
1488 
1489 	for (i = 0; i < sc->vtcon_max_ports; i++) {
1490 		scport = &sc->vtcon_ports[i];
1491 
1492 		port = scport->vcsp_port;
1493 		if (port == NULL)
1494 			continue;
1495 
1496 		VTCON_PORT_LOCK(port);
1497 		vtcon_port_disable_intr(port);
1498 		VTCON_PORT_UNLOCK(port);
1499 	}
1500 }
1501