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