xref: /freebsd/sys/dev/virtio/console/virtio_console.c (revision f9fd7337f63698f33239c58c07bf430198235a22)
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 	{ 0, NULL }
150 };
151 
152 static int	 vtcon_modevent(module_t, int, void *);
153 static void	 vtcon_drain_all(void);
154 
155 static int	 vtcon_probe(device_t);
156 static int	 vtcon_attach(device_t);
157 static int	 vtcon_detach(device_t);
158 static int	 vtcon_config_change(device_t);
159 
160 static void	 vtcon_setup_features(struct vtcon_softc *);
161 static void	 vtcon_negotiate_features(struct vtcon_softc *);
162 static int	 vtcon_alloc_scports(struct vtcon_softc *);
163 static int	 vtcon_alloc_virtqueues(struct vtcon_softc *);
164 static void	 vtcon_read_config(struct vtcon_softc *,
165 		     struct virtio_console_config *);
166 
167 static void	 vtcon_determine_max_ports(struct vtcon_softc *,
168 		     struct virtio_console_config *);
169 static void	 vtcon_destroy_ports(struct vtcon_softc *);
170 static void	 vtcon_stop(struct vtcon_softc *);
171 
172 static int	 vtcon_ctrl_event_enqueue(struct vtcon_softc *,
173 		     struct virtio_console_control *);
174 static int	 vtcon_ctrl_event_create(struct vtcon_softc *);
175 static void	 vtcon_ctrl_event_requeue(struct vtcon_softc *,
176 		     struct virtio_console_control *);
177 static int	 vtcon_ctrl_event_populate(struct vtcon_softc *);
178 static void	 vtcon_ctrl_event_drain(struct vtcon_softc *);
179 static int	 vtcon_ctrl_init(struct vtcon_softc *);
180 static void	 vtcon_ctrl_deinit(struct vtcon_softc *);
181 static void	 vtcon_ctrl_port_add_event(struct vtcon_softc *, int);
182 static void	 vtcon_ctrl_port_remove_event(struct vtcon_softc *, int);
183 static void	 vtcon_ctrl_port_console_event(struct vtcon_softc *, int);
184 static void	 vtcon_ctrl_port_open_event(struct vtcon_softc *, int);
185 static void	 vtcon_ctrl_port_name_event(struct vtcon_softc *, int,
186 		     const char *, size_t);
187 static void	 vtcon_ctrl_process_event(struct vtcon_softc *,
188 		     struct virtio_console_control *, void *, size_t);
189 static void	 vtcon_ctrl_task_cb(void *, int);
190 static void	 vtcon_ctrl_event_intr(void *);
191 static void	 vtcon_ctrl_poll(struct vtcon_softc *,
192 		     struct virtio_console_control *control);
193 static void	 vtcon_ctrl_send_control(struct vtcon_softc *, uint32_t,
194 		     uint16_t, uint16_t);
195 
196 static int	 vtcon_port_enqueue_buf(struct vtcon_port *, void *, size_t);
197 static int	 vtcon_port_create_buf(struct vtcon_port *);
198 static void	 vtcon_port_requeue_buf(struct vtcon_port *, void *);
199 static int	 vtcon_port_populate(struct vtcon_port *);
200 static void	 vtcon_port_destroy(struct vtcon_port *);
201 static int	 vtcon_port_create(struct vtcon_softc *, int);
202 static void	 vtcon_port_dev_alias(struct vtcon_port *, const char *,
203 		     size_t);
204 static void	 vtcon_port_drain_bufs(struct virtqueue *);
205 static void	 vtcon_port_drain(struct vtcon_port *);
206 static void	 vtcon_port_teardown(struct vtcon_port *);
207 static void	 vtcon_port_change_size(struct vtcon_port *, uint16_t,
208 		     uint16_t);
209 static void	 vtcon_port_update_console_size(struct vtcon_softc *);
210 static void	 vtcon_port_enable_intr(struct vtcon_port *);
211 static void	 vtcon_port_disable_intr(struct vtcon_port *);
212 static void	 vtcon_port_in(struct vtcon_port *);
213 static void	 vtcon_port_intr(void *);
214 static void	 vtcon_port_out(struct vtcon_port *, void *, int);
215 static void	 vtcon_port_submit_event(struct vtcon_port *, uint16_t,
216 		     uint16_t);
217 
218 static int	 vtcon_tty_open(struct tty *);
219 static void	 vtcon_tty_close(struct tty *);
220 static void	 vtcon_tty_outwakeup(struct tty *);
221 static void	 vtcon_tty_free(void *);
222 
223 static void	 vtcon_get_console_size(struct vtcon_softc *, uint16_t *,
224 		     uint16_t *);
225 
226 static void	 vtcon_enable_interrupts(struct vtcon_softc *);
227 static void	 vtcon_disable_interrupts(struct vtcon_softc *);
228 
229 static int	 vtcon_pending_free;
230 
231 static struct ttydevsw vtcon_tty_class = {
232 	.tsw_flags	= 0,
233 	.tsw_open	= vtcon_tty_open,
234 	.tsw_close	= vtcon_tty_close,
235 	.tsw_outwakeup	= vtcon_tty_outwakeup,
236 	.tsw_free	= vtcon_tty_free,
237 };
238 
239 static device_method_t vtcon_methods[] = {
240 	/* Device methods. */
241 	DEVMETHOD(device_probe,		vtcon_probe),
242 	DEVMETHOD(device_attach,	vtcon_attach),
243 	DEVMETHOD(device_detach,	vtcon_detach),
244 
245 	/* VirtIO methods. */
246 	DEVMETHOD(virtio_config_change,	vtcon_config_change),
247 
248 	DEVMETHOD_END
249 };
250 
251 static driver_t vtcon_driver = {
252 	"vtcon",
253 	vtcon_methods,
254 	sizeof(struct vtcon_softc)
255 };
256 static devclass_t vtcon_devclass;
257 
258 DRIVER_MODULE(virtio_console, virtio_mmio, vtcon_driver, vtcon_devclass,
259     vtcon_modevent, 0);
260 DRIVER_MODULE(virtio_console, virtio_pci, vtcon_driver, vtcon_devclass,
261     vtcon_modevent, 0);
262 MODULE_VERSION(virtio_console, 1);
263 MODULE_DEPEND(virtio_console, virtio, 1, 1, 1);
264 
265 VIRTIO_SIMPLE_PNPTABLE(virtio_console, VIRTIO_ID_CONSOLE,
266     "VirtIO Console Adapter");
267 VIRTIO_SIMPLE_PNPINFO(virtio_mmio, virtio_console);
268 VIRTIO_SIMPLE_PNPINFO(virtio_pci, virtio_console);
269 
270 static int
271 vtcon_modevent(module_t mod, int type, void *unused)
272 {
273 	int error;
274 
275 	switch (type) {
276 	case MOD_LOAD:
277 		error = 0;
278 		break;
279 	case MOD_QUIESCE:
280 		error = 0;
281 		break;
282 	case MOD_UNLOAD:
283 		vtcon_drain_all();
284 		error = 0;
285 		break;
286 	case MOD_SHUTDOWN:
287 		error = 0;
288 		break;
289 	default:
290 		error = EOPNOTSUPP;
291 		break;
292 	}
293 
294 	return (error);
295 }
296 
297 static void
298 vtcon_drain_all(void)
299 {
300 	int first;
301 
302 	for (first = 1; vtcon_pending_free != 0; first = 0) {
303 		if (first != 0) {
304 			printf("virtio_console: Waiting for all detached TTY "
305 			    "devices to have open fds closed.\n");
306 		}
307 		pause("vtcondra", hz);
308 	}
309 }
310 
311 static int
312 vtcon_probe(device_t dev)
313 {
314 	return (VIRTIO_SIMPLE_PROBE(dev, virtio_console));
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 		if (i == 1) {
514 			/* The control virtqueues are after the first port. */
515 			VQ_ALLOC_INFO_INIT(&info[idx], 0,
516 			    vtcon_ctrl_event_intr, sc, &sc->vtcon_ctrl_rxvq,
517 			    "%s-control rx", device_get_nameunit(dev));
518 			VQ_ALLOC_INFO_INIT(&info[idx+1], 0,
519 			    NULL, sc, &sc->vtcon_ctrl_txvq,
520 			    "%s-control tx", device_get_nameunit(dev));
521 			continue;
522 		}
523 
524 		scport = &sc->vtcon_ports[portidx];
525 
526 		VQ_ALLOC_INFO_INIT(&info[idx], 0, vtcon_port_intr,
527 		    scport, &scport->vcsp_invq, "%s-port%d in",
528 		    device_get_nameunit(dev), i);
529 		VQ_ALLOC_INFO_INIT(&info[idx+1], 0, NULL,
530 		    NULL, &scport->vcsp_outvq, "%s-port%d out",
531 		    device_get_nameunit(dev), i);
532 
533 		portidx++;
534 	}
535 
536 	error = virtio_alloc_virtqueues(dev, 0, nvqs, info);
537 	free(info, M_TEMP);
538 
539 	return (error);
540 }
541 
542 static void
543 vtcon_determine_max_ports(struct vtcon_softc *sc,
544     struct virtio_console_config *concfg)
545 {
546 
547 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
548 		sc->vtcon_max_ports =
549 		    min(concfg->max_nr_ports, VTCON_MAX_PORTS);
550 		if (sc->vtcon_max_ports == 0)
551 			sc->vtcon_max_ports = 1;
552 	} else
553 		sc->vtcon_max_ports = 1;
554 }
555 
556 static void
557 vtcon_destroy_ports(struct vtcon_softc *sc)
558 {
559 	struct vtcon_softc_port *scport;
560 	struct vtcon_port *port;
561 	struct virtqueue *vq;
562 	int i;
563 
564 	if (sc->vtcon_ports == NULL)
565 		return;
566 
567 	VTCON_LOCK(sc);
568 	for (i = 0; i < sc->vtcon_max_ports; i++) {
569 		scport = &sc->vtcon_ports[i];
570 
571 		port = scport->vcsp_port;
572 		if (port != NULL) {
573 			scport->vcsp_port = NULL;
574 			VTCON_PORT_LOCK(port);
575 			VTCON_UNLOCK(sc);
576 			vtcon_port_teardown(port);
577 			VTCON_LOCK(sc);
578 		}
579 
580 		vq = scport->vcsp_invq;
581 		if (vq != NULL)
582 			vtcon_port_drain_bufs(vq);
583 	}
584 	VTCON_UNLOCK(sc);
585 
586 	free(sc->vtcon_ports, M_DEVBUF);
587 	sc->vtcon_ports = NULL;
588 }
589 
590 static void
591 vtcon_stop(struct vtcon_softc *sc)
592 {
593 
594 	vtcon_disable_interrupts(sc);
595 	virtio_stop(sc->vtcon_dev);
596 }
597 
598 static int
599 vtcon_ctrl_event_enqueue(struct vtcon_softc *sc,
600     struct virtio_console_control *control)
601 {
602 	struct sglist_seg segs[2];
603 	struct sglist sg;
604 	struct virtqueue *vq;
605 	int error;
606 
607 	vq = sc->vtcon_ctrl_rxvq;
608 
609 	sglist_init(&sg, 2, segs);
610 	error = sglist_append(&sg, control, VTCON_CTRL_BUFSZ);
611 	KASSERT(error == 0, ("%s: error %d adding control to sglist",
612 	    __func__, error));
613 
614 	return (virtqueue_enqueue(vq, control, &sg, 0, sg.sg_nseg));
615 }
616 
617 static int
618 vtcon_ctrl_event_create(struct vtcon_softc *sc)
619 {
620 	struct virtio_console_control *control;
621 	int error;
622 
623 	control = malloc(VTCON_CTRL_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
624 	if (control == NULL)
625 		return (ENOMEM);
626 
627 	error = vtcon_ctrl_event_enqueue(sc, control);
628 	if (error)
629 		free(control, M_DEVBUF);
630 
631 	return (error);
632 }
633 
634 static void
635 vtcon_ctrl_event_requeue(struct vtcon_softc *sc,
636     struct virtio_console_control *control)
637 {
638 	int error;
639 
640 	bzero(control, VTCON_CTRL_BUFSZ);
641 
642 	error = vtcon_ctrl_event_enqueue(sc, control);
643 	KASSERT(error == 0,
644 	    ("%s: cannot requeue control buffer %d", __func__, error));
645 }
646 
647 static int
648 vtcon_ctrl_event_populate(struct vtcon_softc *sc)
649 {
650 	struct virtqueue *vq;
651 	int nbufs, error;
652 
653 	vq = sc->vtcon_ctrl_rxvq;
654 	error = ENOSPC;
655 
656 	for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
657 		error = vtcon_ctrl_event_create(sc);
658 		if (error)
659 			break;
660 	}
661 
662 	if (nbufs > 0) {
663 		virtqueue_notify(vq);
664 		error = 0;
665 	}
666 
667 	return (error);
668 }
669 
670 static void
671 vtcon_ctrl_event_drain(struct vtcon_softc *sc)
672 {
673 	struct virtio_console_control *control;
674 	struct virtqueue *vq;
675 	int last;
676 
677 	vq = sc->vtcon_ctrl_rxvq;
678 	last = 0;
679 
680 	if (vq == NULL)
681 		return;
682 
683 	VTCON_LOCK(sc);
684 	while ((control = virtqueue_drain(vq, &last)) != NULL)
685 		free(control, M_DEVBUF);
686 	VTCON_UNLOCK(sc);
687 }
688 
689 static int
690 vtcon_ctrl_init(struct vtcon_softc *sc)
691 {
692 	int error;
693 
694 	error = vtcon_ctrl_event_populate(sc);
695 
696 	return (error);
697 }
698 
699 static void
700 vtcon_ctrl_deinit(struct vtcon_softc *sc)
701 {
702 
703 	vtcon_ctrl_event_drain(sc);
704 }
705 
706 static void
707 vtcon_ctrl_port_add_event(struct vtcon_softc *sc, int id)
708 {
709 	device_t dev;
710 	int error;
711 
712 	dev = sc->vtcon_dev;
713 
714 	/* This single thread only way for ports to be created. */
715 	if (sc->vtcon_ports[id].vcsp_port != NULL) {
716 		device_printf(dev, "%s: adding port %d, but already exists\n",
717 		    __func__, id);
718 		return;
719 	}
720 
721 	error = vtcon_port_create(sc, id);
722 	if (error) {
723 		device_printf(dev, "%s: cannot create port %d: %d\n",
724 		    __func__, id, error);
725 		vtcon_ctrl_send_control(sc, id, VIRTIO_CONSOLE_PORT_READY, 0);
726 		return;
727 	}
728 }
729 
730 static void
731 vtcon_ctrl_port_remove_event(struct vtcon_softc *sc, int id)
732 {
733 	device_t dev;
734 	struct vtcon_softc_port *scport;
735 	struct vtcon_port *port;
736 
737 	dev = sc->vtcon_dev;
738 	scport = &sc->vtcon_ports[id];
739 
740 	VTCON_LOCK(sc);
741 	port = scport->vcsp_port;
742 	if (port == NULL) {
743 		VTCON_UNLOCK(sc);
744 		device_printf(dev, "%s: remove port %d, but does not exist\n",
745 		    __func__, id);
746 		return;
747 	}
748 
749 	scport->vcsp_port = NULL;
750 	VTCON_PORT_LOCK(port);
751 	VTCON_UNLOCK(sc);
752 	vtcon_port_teardown(port);
753 }
754 
755 static void
756 vtcon_ctrl_port_console_event(struct vtcon_softc *sc, int id)
757 {
758 	device_t dev;
759 	struct vtcon_softc_port *scport;
760 	struct vtcon_port *port;
761 
762 	dev = sc->vtcon_dev;
763 	scport = &sc->vtcon_ports[id];
764 
765 	VTCON_LOCK(sc);
766 	port = scport->vcsp_port;
767 	if (port == NULL) {
768 		VTCON_UNLOCK(sc);
769 		device_printf(dev, "%s: console port %d, but does not exist\n",
770 		    __func__, id);
771 		return;
772 	}
773 
774 	VTCON_PORT_LOCK(port);
775 	VTCON_UNLOCK(sc);
776 	port->vtcport_flags |= VTCON_PORT_FLAG_CONSOLE;
777 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
778 	VTCON_PORT_UNLOCK(port);
779 }
780 
781 static void
782 vtcon_ctrl_port_open_event(struct vtcon_softc *sc, int id)
783 {
784 	device_t dev;
785 	struct vtcon_softc_port *scport;
786 	struct vtcon_port *port;
787 
788 	dev = sc->vtcon_dev;
789 	scport = &sc->vtcon_ports[id];
790 
791 	VTCON_LOCK(sc);
792 	port = scport->vcsp_port;
793 	if (port == NULL) {
794 		VTCON_UNLOCK(sc);
795 		device_printf(dev, "%s: open port %d, but does not exist\n",
796 		    __func__, id);
797 		return;
798 	}
799 
800 	VTCON_PORT_LOCK(port);
801 	VTCON_UNLOCK(sc);
802 	vtcon_port_enable_intr(port);
803 	VTCON_PORT_UNLOCK(port);
804 }
805 
806 static void
807 vtcon_ctrl_port_name_event(struct vtcon_softc *sc, int id, const char *name,
808     size_t len)
809 {
810 	device_t dev;
811 	struct vtcon_softc_port *scport;
812 	struct vtcon_port *port;
813 
814 	dev = sc->vtcon_dev;
815 	scport = &sc->vtcon_ports[id];
816 
817 	/*
818 	 * The VirtIO specification says the NUL terminator is not included in
819 	 * the length, but QEMU includes it. Adjust the length if needed.
820 	 */
821 	if (name == NULL || len == 0)
822 		return;
823 	if (name[len - 1] == '\0') {
824 		len--;
825 		if (len == 0)
826 			return;
827 	}
828 
829 	VTCON_LOCK(sc);
830 	port = scport->vcsp_port;
831 	if (port == NULL) {
832 		VTCON_UNLOCK(sc);
833 		device_printf(dev, "%s: name port %d, but does not exist\n",
834 		    __func__, id);
835 		return;
836 	}
837 
838 	VTCON_PORT_LOCK(port);
839 	VTCON_UNLOCK(sc);
840 	vtcon_port_dev_alias(port, name, len);
841 	VTCON_PORT_UNLOCK(port);
842 }
843 
844 static void
845 vtcon_ctrl_process_event(struct vtcon_softc *sc,
846     struct virtio_console_control *control, void *data, size_t data_len)
847 {
848 	device_t dev;
849 	int id;
850 
851 	dev = sc->vtcon_dev;
852 	id = control->id;
853 
854 	if (id < 0 || id >= sc->vtcon_max_ports) {
855 		device_printf(dev, "%s: invalid port ID %d\n", __func__, id);
856 		return;
857 	}
858 
859 	switch (control->event) {
860 	case VIRTIO_CONSOLE_PORT_ADD:
861 		vtcon_ctrl_port_add_event(sc, id);
862 		break;
863 
864 	case VIRTIO_CONSOLE_PORT_REMOVE:
865 		vtcon_ctrl_port_remove_event(sc, id);
866 		break;
867 
868 	case VIRTIO_CONSOLE_CONSOLE_PORT:
869 		vtcon_ctrl_port_console_event(sc, id);
870 		break;
871 
872 	case VIRTIO_CONSOLE_RESIZE:
873 		break;
874 
875 	case VIRTIO_CONSOLE_PORT_OPEN:
876 		vtcon_ctrl_port_open_event(sc, id);
877 		break;
878 
879 	case VIRTIO_CONSOLE_PORT_NAME:
880 		vtcon_ctrl_port_name_event(sc, id, (const char *)data, data_len);
881 		break;
882 	}
883 }
884 
885 static void
886 vtcon_ctrl_task_cb(void *xsc, int pending)
887 {
888 	struct vtcon_softc *sc;
889 	struct virtqueue *vq;
890 	struct virtio_console_control *control;
891 	void *data;
892 	size_t data_len;
893 	int detached;
894 	uint32_t len;
895 
896 	sc = xsc;
897 	vq = sc->vtcon_ctrl_rxvq;
898 
899 	VTCON_LOCK(sc);
900 
901 	while ((detached = (sc->vtcon_flags & VTCON_FLAG_DETACHED)) == 0) {
902 		control = virtqueue_dequeue(vq, &len);
903 		if (control == NULL)
904 			break;
905 
906 		if (len > sizeof(struct virtio_console_control)) {
907 			data = (void *) &control[1];
908 			data_len = len - sizeof(struct virtio_console_control);
909 		} else {
910 			data = NULL;
911 			data_len = 0;
912 		}
913 
914 		VTCON_UNLOCK(sc);
915 		vtcon_ctrl_process_event(sc, control, data, data_len);
916 		VTCON_LOCK(sc);
917 		vtcon_ctrl_event_requeue(sc, control);
918 	}
919 
920 	if (!detached) {
921 		virtqueue_notify(vq);
922 		if (virtqueue_enable_intr(vq) != 0)
923 			taskqueue_enqueue(taskqueue_thread,
924 			    &sc->vtcon_ctrl_task);
925 	}
926 
927 	VTCON_UNLOCK(sc);
928 }
929 
930 static void
931 vtcon_ctrl_event_intr(void *xsc)
932 {
933 	struct vtcon_softc *sc;
934 
935 	sc = xsc;
936 
937 	/*
938 	 * Only some events require us to potentially block, but it
939 	 * easier to just defer all event handling to the taskqueue.
940 	 */
941 	taskqueue_enqueue(taskqueue_thread, &sc->vtcon_ctrl_task);
942 }
943 
944 static void
945 vtcon_ctrl_poll(struct vtcon_softc *sc,
946     struct virtio_console_control *control)
947 {
948 	struct sglist_seg segs[2];
949 	struct sglist sg;
950 	struct virtqueue *vq;
951 	int error;
952 
953 	vq = sc->vtcon_ctrl_txvq;
954 
955 	sglist_init(&sg, 2, segs);
956 	error = sglist_append(&sg, control,
957 	    sizeof(struct virtio_console_control));
958 	KASSERT(error == 0, ("%s: error %d adding control to sglist",
959 	    __func__, error));
960 
961 	/*
962 	 * We cannot use the softc lock to serialize access to this
963 	 * virtqueue since this is called from the tty layer with the
964 	 * port lock held. Acquiring the softc would violate our lock
965 	 * ordering.
966 	 */
967 	VTCON_CTRL_TX_LOCK(sc);
968 	KASSERT(virtqueue_empty(vq),
969 	    ("%s: virtqueue is not emtpy", __func__));
970 	error = virtqueue_enqueue(vq, control, &sg, sg.sg_nseg, 0);
971 	if (error == 0) {
972 		virtqueue_notify(vq);
973 		virtqueue_poll(vq, NULL);
974 	}
975 	VTCON_CTRL_TX_UNLOCK(sc);
976 }
977 
978 static void
979 vtcon_ctrl_send_control(struct vtcon_softc *sc, uint32_t portid,
980     uint16_t event, uint16_t value)
981 {
982 	struct virtio_console_control control;
983 
984 	if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0)
985 		return;
986 
987 	control.id = portid;
988 	control.event = event;
989 	control.value = value;
990 
991 	vtcon_ctrl_poll(sc, &control);
992 }
993 
994 static int
995 vtcon_port_enqueue_buf(struct vtcon_port *port, void *buf, size_t len)
996 {
997 	struct sglist_seg segs[2];
998 	struct sglist sg;
999 	struct virtqueue *vq;
1000 	int error;
1001 
1002 	vq = port->vtcport_invq;
1003 
1004 	sglist_init(&sg, 2, segs);
1005 	error = sglist_append(&sg, buf, len);
1006 	KASSERT(error == 0,
1007 	    ("%s: error %d adding buffer to sglist", __func__, error));
1008 
1009 	error = virtqueue_enqueue(vq, buf, &sg, 0, sg.sg_nseg);
1010 
1011 	return (error);
1012 }
1013 
1014 static int
1015 vtcon_port_create_buf(struct vtcon_port *port)
1016 {
1017 	void *buf;
1018 	int error;
1019 
1020 	buf = malloc(VTCON_BULK_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
1021 	if (buf == NULL)
1022 		return (ENOMEM);
1023 
1024 	error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1025 	if (error)
1026 		free(buf, M_DEVBUF);
1027 
1028 	return (error);
1029 }
1030 
1031 static void
1032 vtcon_port_requeue_buf(struct vtcon_port *port, void *buf)
1033 {
1034 	int error;
1035 
1036 	error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1037 	KASSERT(error == 0,
1038 	    ("%s: cannot requeue input buffer %d", __func__, error));
1039 }
1040 
1041 static int
1042 vtcon_port_populate(struct vtcon_port *port)
1043 {
1044 	struct virtqueue *vq;
1045 	int nbufs, error;
1046 
1047 	vq = port->vtcport_invq;
1048 	error = ENOSPC;
1049 
1050 	for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1051 		error = vtcon_port_create_buf(port);
1052 		if (error)
1053 			break;
1054 	}
1055 
1056 	if (nbufs > 0) {
1057 		virtqueue_notify(vq);
1058 		error = 0;
1059 	}
1060 
1061 	return (error);
1062 }
1063 
1064 static void
1065 vtcon_port_destroy(struct vtcon_port *port)
1066 {
1067 
1068 	port->vtcport_sc = NULL;
1069 	port->vtcport_scport = NULL;
1070 	port->vtcport_invq = NULL;
1071 	port->vtcport_outvq = NULL;
1072 	port->vtcport_id = -1;
1073 	mtx_destroy(&port->vtcport_mtx);
1074 	free(port, M_DEVBUF);
1075 }
1076 
1077 static int
1078 vtcon_port_init_vqs(struct vtcon_port *port)
1079 {
1080 	struct vtcon_softc_port *scport;
1081 	int error;
1082 
1083 	scport = port->vtcport_scport;
1084 
1085 	port->vtcport_invq = scport->vcsp_invq;
1086 	port->vtcport_outvq = scport->vcsp_outvq;
1087 
1088 	/*
1089 	 * Free any data left over from when this virtqueue was in use by a
1090 	 * prior port. We have not yet notified the host that the port is
1091 	 * ready, so assume nothing in the virtqueue can be for us.
1092 	 */
1093 	vtcon_port_drain(port);
1094 
1095 	KASSERT(virtqueue_empty(port->vtcport_invq),
1096 	    ("%s: in virtqueue is not empty", __func__));
1097 	KASSERT(virtqueue_empty(port->vtcport_outvq),
1098 	    ("%s: out virtqueue is not empty", __func__));
1099 
1100 	error = vtcon_port_populate(port);
1101 	if (error)
1102 		return (error);
1103 
1104 	return (0);
1105 }
1106 
1107 static int
1108 vtcon_port_create(struct vtcon_softc *sc, int id)
1109 {
1110 	device_t dev;
1111 	struct vtcon_softc_port *scport;
1112 	struct vtcon_port *port;
1113 	int error;
1114 
1115 	dev = sc->vtcon_dev;
1116 	scport = &sc->vtcon_ports[id];
1117 
1118 	VTCON_ASSERT_VALID_PORTID(sc, id);
1119 	MPASS(scport->vcsp_port == NULL);
1120 
1121 	port = malloc(sizeof(struct vtcon_port), M_DEVBUF, M_NOWAIT | M_ZERO);
1122 	if (port == NULL)
1123 		return (ENOMEM);
1124 
1125 	port->vtcport_sc = sc;
1126 	port->vtcport_scport = scport;
1127 	port->vtcport_id = id;
1128 	mtx_init(&port->vtcport_mtx, "vtcpmtx", NULL, MTX_DEF);
1129 	port->vtcport_tty = tty_alloc_mutex(&vtcon_tty_class, port,
1130 	    &port->vtcport_mtx);
1131 
1132 	error = vtcon_port_init_vqs(port);
1133 	if (error) {
1134 		VTCON_PORT_LOCK(port);
1135 		vtcon_port_teardown(port);
1136 		return (error);
1137 	}
1138 
1139 	VTCON_LOCK(sc);
1140 	VTCON_PORT_LOCK(port);
1141 	scport->vcsp_port = port;
1142 	vtcon_port_enable_intr(port);
1143 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_READY, 1);
1144 	VTCON_PORT_UNLOCK(port);
1145 	VTCON_UNLOCK(sc);
1146 
1147 	tty_makedev(port->vtcport_tty, NULL, "%s%r.%r", VTCON_TTY_PREFIX,
1148 	    device_get_unit(dev), id);
1149 
1150 	return (0);
1151 }
1152 
1153 static void
1154 vtcon_port_dev_alias(struct vtcon_port *port, const char *name, size_t len)
1155 {
1156 	struct vtcon_softc *sc;
1157 	struct cdev *pdev;
1158 	struct tty *tp;
1159 	int i, error;
1160 
1161 	sc = port->vtcport_sc;
1162 	tp = port->vtcport_tty;
1163 
1164 	if (port->vtcport_flags & VTCON_PORT_FLAG_ALIAS)
1165 		return;
1166 
1167 	/* Port name is UTF-8, but we can only handle ASCII. */
1168 	for (i = 0; i < len; i++) {
1169 		if (!isascii(name[i]))
1170 			return;
1171 	}
1172 
1173 	/*
1174 	 * Port name may not conform to the devfs requirements so we cannot use
1175 	 * tty_makealias() because the MAKEDEV_CHECKNAME flag must be specified.
1176 	 */
1177 	error = make_dev_alias_p(MAKEDEV_NOWAIT | MAKEDEV_CHECKNAME, &pdev,
1178 	    tp->t_dev, "%s/%*s", VTCON_TTY_ALIAS_PREFIX, (int)len, name);
1179 	if (error) {
1180 		device_printf(sc->vtcon_dev,
1181 		    "%s: cannot make dev alias (%s/%*s) error %d\n", __func__,
1182 		    VTCON_TTY_ALIAS_PREFIX, (int)len, name, error);
1183 	} else
1184 		port->vtcport_flags |= VTCON_PORT_FLAG_ALIAS;
1185 }
1186 
1187 static void
1188 vtcon_port_drain_bufs(struct virtqueue *vq)
1189 {
1190 	void *buf;
1191 	int last;
1192 
1193 	last = 0;
1194 
1195 	while ((buf = virtqueue_drain(vq, &last)) != NULL)
1196 		free(buf, M_DEVBUF);
1197 }
1198 
1199 static void
1200 vtcon_port_drain(struct vtcon_port *port)
1201 {
1202 
1203 	vtcon_port_drain_bufs(port->vtcport_invq);
1204 }
1205 
1206 static void
1207 vtcon_port_teardown(struct vtcon_port *port)
1208 {
1209 	struct tty *tp;
1210 
1211 	tp = port->vtcport_tty;
1212 
1213 	port->vtcport_flags |= VTCON_PORT_FLAG_GONE;
1214 
1215 	if (tp != NULL) {
1216 		atomic_add_int(&vtcon_pending_free, 1);
1217 		tty_rel_gone(tp);
1218 	} else
1219 		vtcon_port_destroy(port);
1220 }
1221 
1222 static void
1223 vtcon_port_change_size(struct vtcon_port *port, uint16_t cols, uint16_t rows)
1224 {
1225 	struct tty *tp;
1226 	struct winsize sz;
1227 
1228 	tp = port->vtcport_tty;
1229 
1230 	if (tp == NULL)
1231 		return;
1232 
1233 	bzero(&sz, sizeof(struct winsize));
1234 	sz.ws_col = cols;
1235 	sz.ws_row = rows;
1236 
1237 	tty_set_winsize(tp, &sz);
1238 }
1239 
1240 static void
1241 vtcon_port_update_console_size(struct vtcon_softc *sc)
1242 {
1243 	struct vtcon_port *port;
1244 	struct vtcon_softc_port *scport;
1245 	uint16_t cols, rows;
1246 
1247 	vtcon_get_console_size(sc, &cols, &rows);
1248 
1249 	/*
1250 	 * For now, assume the first (only) port is the console. Note
1251 	 * QEMU does not implement this feature yet.
1252 	 */
1253 	scport = &sc->vtcon_ports[0];
1254 
1255 	VTCON_LOCK(sc);
1256 	port = scport->vcsp_port;
1257 
1258 	if (port != NULL) {
1259 		VTCON_PORT_LOCK(port);
1260 		VTCON_UNLOCK(sc);
1261 		vtcon_port_change_size(port, cols, rows);
1262 		VTCON_PORT_UNLOCK(port);
1263 	} else
1264 		VTCON_UNLOCK(sc);
1265 }
1266 
1267 static void
1268 vtcon_port_enable_intr(struct vtcon_port *port)
1269 {
1270 
1271 	/*
1272 	 * NOTE: The out virtqueue is always polled, so its interrupt
1273 	 * kept disabled.
1274 	 */
1275 	virtqueue_enable_intr(port->vtcport_invq);
1276 }
1277 
1278 static void
1279 vtcon_port_disable_intr(struct vtcon_port *port)
1280 {
1281 
1282 	if (port->vtcport_invq != NULL)
1283 		virtqueue_disable_intr(port->vtcport_invq);
1284 	if (port->vtcport_outvq != NULL)
1285 		virtqueue_disable_intr(port->vtcport_outvq);
1286 }
1287 
1288 static void
1289 vtcon_port_in(struct vtcon_port *port)
1290 {
1291 	struct virtqueue *vq;
1292 	struct tty *tp;
1293 	char *buf;
1294 	uint32_t len;
1295 	int i, deq;
1296 
1297 	tp = port->vtcport_tty;
1298 	vq = port->vtcport_invq;
1299 
1300 again:
1301 	deq = 0;
1302 
1303 	while ((buf = virtqueue_dequeue(vq, &len)) != NULL) {
1304 		for (i = 0; i < len; i++) {
1305 #if defined(KDB)
1306 			if (port->vtcport_flags & VTCON_PORT_FLAG_CONSOLE)
1307 				kdb_alt_break(buf[i],
1308 				    &port->vtcport_alt_break_state);
1309 #endif
1310 			ttydisc_rint(tp, buf[i], 0);
1311 		}
1312 		vtcon_port_requeue_buf(port, buf);
1313 		deq++;
1314 	}
1315 	ttydisc_rint_done(tp);
1316 
1317 	if (deq > 0)
1318 		virtqueue_notify(vq);
1319 
1320 	if (virtqueue_enable_intr(vq) != 0)
1321 		goto again;
1322 }
1323 
1324 static void
1325 vtcon_port_intr(void *scportx)
1326 {
1327 	struct vtcon_softc_port *scport;
1328 	struct vtcon_softc *sc;
1329 	struct vtcon_port *port;
1330 
1331 	scport = scportx;
1332 	sc = scport->vcsp_sc;
1333 
1334 	VTCON_LOCK(sc);
1335 	port = scport->vcsp_port;
1336 	if (port == NULL) {
1337 		VTCON_UNLOCK(sc);
1338 		return;
1339 	}
1340 	VTCON_PORT_LOCK(port);
1341 	VTCON_UNLOCK(sc);
1342 	if ((port->vtcport_flags & VTCON_PORT_FLAG_GONE) == 0)
1343 		vtcon_port_in(port);
1344 	VTCON_PORT_UNLOCK(port);
1345 }
1346 
1347 static void
1348 vtcon_port_out(struct vtcon_port *port, void *buf, int bufsize)
1349 {
1350 	struct sglist_seg segs[2];
1351 	struct sglist sg;
1352 	struct virtqueue *vq;
1353 	int error;
1354 
1355 	vq = port->vtcport_outvq;
1356 	KASSERT(virtqueue_empty(vq),
1357 	    ("%s: port %p out virtqueue not emtpy", __func__, port));
1358 
1359 	sglist_init(&sg, 2, segs);
1360 	error = sglist_append(&sg, buf, bufsize);
1361 	KASSERT(error == 0, ("%s: error %d adding buffer to sglist",
1362 	    __func__, error));
1363 
1364 	error = virtqueue_enqueue(vq, buf, &sg, sg.sg_nseg, 0);
1365 	if (error == 0) {
1366 		virtqueue_notify(vq);
1367 		virtqueue_poll(vq, NULL);
1368 	}
1369 }
1370 
1371 static void
1372 vtcon_port_submit_event(struct vtcon_port *port, uint16_t event,
1373     uint16_t value)
1374 {
1375 	struct vtcon_softc *sc;
1376 
1377 	sc = port->vtcport_sc;
1378 
1379 	vtcon_ctrl_send_control(sc, port->vtcport_id, event, value);
1380 }
1381 
1382 static int
1383 vtcon_tty_open(struct tty *tp)
1384 {
1385 	struct vtcon_port *port;
1386 
1387 	port = tty_softc(tp);
1388 
1389 	if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1390 		return (ENXIO);
1391 
1392 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1393 
1394 	return (0);
1395 }
1396 
1397 static void
1398 vtcon_tty_close(struct tty *tp)
1399 {
1400 	struct vtcon_port *port;
1401 
1402 	port = tty_softc(tp);
1403 
1404 	if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1405 		return;
1406 
1407 	vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1408 }
1409 
1410 static void
1411 vtcon_tty_outwakeup(struct tty *tp)
1412 {
1413 	struct vtcon_port *port;
1414 	char buf[VTCON_BULK_BUFSZ];
1415 	int len;
1416 
1417 	port = tty_softc(tp);
1418 
1419 	if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1420 		return;
1421 
1422 	while ((len = ttydisc_getc(tp, buf, sizeof(buf))) != 0)
1423 		vtcon_port_out(port, buf, len);
1424 }
1425 
1426 static void
1427 vtcon_tty_free(void *xport)
1428 {
1429 	struct vtcon_port *port;
1430 
1431 	port = xport;
1432 
1433 	vtcon_port_destroy(port);
1434 	atomic_subtract_int(&vtcon_pending_free, 1);
1435 }
1436 
1437 static void
1438 vtcon_get_console_size(struct vtcon_softc *sc, uint16_t *cols, uint16_t *rows)
1439 {
1440 	struct virtio_console_config concfg;
1441 
1442 	KASSERT(sc->vtcon_flags & VTCON_FLAG_SIZE,
1443 	    ("%s: size feature not negotiated", __func__));
1444 
1445 	vtcon_read_config(sc, &concfg);
1446 
1447 	*cols = concfg.cols;
1448 	*rows = concfg.rows;
1449 }
1450 
1451 static void
1452 vtcon_enable_interrupts(struct vtcon_softc *sc)
1453 {
1454 	struct vtcon_softc_port *scport;
1455 	struct vtcon_port *port;
1456 	int i;
1457 
1458 	VTCON_LOCK(sc);
1459 
1460 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1461 		virtqueue_enable_intr(sc->vtcon_ctrl_rxvq);
1462 
1463 	for (i = 0; i < sc->vtcon_max_ports; i++) {
1464 		scport = &sc->vtcon_ports[i];
1465 
1466 		port = scport->vcsp_port;
1467 		if (port == NULL)
1468 			continue;
1469 
1470 		VTCON_PORT_LOCK(port);
1471 		vtcon_port_enable_intr(port);
1472 		VTCON_PORT_UNLOCK(port);
1473 	}
1474 
1475 	VTCON_UNLOCK(sc);
1476 }
1477 
1478 static void
1479 vtcon_disable_interrupts(struct vtcon_softc *sc)
1480 {
1481 	struct vtcon_softc_port *scport;
1482 	struct vtcon_port *port;
1483 	int i;
1484 
1485 	VTCON_LOCK_ASSERT(sc);
1486 
1487 	if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1488 		virtqueue_disable_intr(sc->vtcon_ctrl_rxvq);
1489 
1490 	for (i = 0; i < sc->vtcon_max_ports; i++) {
1491 		scport = &sc->vtcon_ports[i];
1492 
1493 		port = scport->vcsp_port;
1494 		if (port == NULL)
1495 			continue;
1496 
1497 		VTCON_PORT_LOCK(port);
1498 		vtcon_port_disable_intr(port);
1499 		VTCON_PORT_UNLOCK(port);
1500 	}
1501 }
1502