bvm_console.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2011 NetApp, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: stable/11/sys/dev/bvm/bvm_console.c 330897 2018-03-14 03:19:51Z eadler $
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/11/sys/dev/bvm/bvm_console.c 330897 2018-03-14 03:19:51Z eadler $");
33
34#include <sys/param.h>
35#include <sys/kernel.h>
36#include <sys/systm.h>
37#include <sys/types.h>
38#include <sys/cons.h>
39#include <sys/tty.h>
40#include <sys/reboot.h>
41#include <sys/bus.h>
42
43#include <sys/kdb.h>
44#include <ddb/ddb.h>
45
46#ifndef	BVMCONS_POLL_HZ
47#define	BVMCONS_POLL_HZ	4
48#endif
49#define BVMBURSTLEN	16	/* max number of bytes to write in one chunk */
50
51static tsw_open_t bvm_tty_open;
52static tsw_close_t bvm_tty_close;
53static tsw_outwakeup_t bvm_tty_outwakeup;
54
55static struct ttydevsw bvm_ttydevsw = {
56	.tsw_flags	= TF_NOPREFIX,
57	.tsw_open	= bvm_tty_open,
58	.tsw_close	= bvm_tty_close,
59	.tsw_outwakeup	= bvm_tty_outwakeup,
60};
61
62static int			polltime;
63static struct callout		bvm_timer;
64
65#if defined(KDB)
66static int			alt_break_state;
67#endif
68
69#define	BVM_CONS_PORT	0x220
70static int bvm_cons_port = BVM_CONS_PORT;
71
72#define BVM_CONS_SIG	('b' << 8 | 'v')
73
74static void	bvm_timeout(void *);
75
76static cn_probe_t	bvm_cnprobe;
77static cn_init_t	bvm_cninit;
78static cn_term_t	bvm_cnterm;
79static cn_getc_t	bvm_cngetc;
80static cn_putc_t	bvm_cnputc;
81static cn_grab_t 	bvm_cngrab;
82static cn_ungrab_t 	bvm_cnungrab;
83
84CONSOLE_DRIVER(bvm);
85
86static int
87bvm_rcons(u_char *ch)
88{
89	int c;
90
91	c = inl(bvm_cons_port);
92	if (c != -1) {
93		*ch = (u_char)c;
94		return (0);
95	} else
96		return (-1);
97}
98
99static void
100bvm_wcons(u_char ch)
101{
102
103	outl(bvm_cons_port, ch);
104}
105
106static void
107cn_drvinit(void *unused)
108{
109	struct tty *tp;
110
111	if (bvm_consdev.cn_pri != CN_DEAD) {
112		tp = tty_alloc(&bvm_ttydevsw, NULL);
113		callout_init_mtx(&bvm_timer, tty_getlock(tp), 0);
114		tty_makedev(tp, NULL, "bvmcons");
115	}
116}
117
118static int
119bvm_tty_open(struct tty *tp)
120{
121	polltime = hz / BVMCONS_POLL_HZ;
122	if (polltime < 1)
123		polltime = 1;
124	callout_reset(&bvm_timer, polltime, bvm_timeout, tp);
125
126	return (0);
127}
128
129static void
130bvm_tty_close(struct tty *tp)
131{
132
133	tty_lock_assert(tp, MA_OWNED);
134	callout_stop(&bvm_timer);
135}
136
137static void
138bvm_tty_outwakeup(struct tty *tp)
139{
140	int len, written;
141	u_char buf[BVMBURSTLEN];
142
143	for (;;) {
144		len = ttydisc_getc(tp, buf, sizeof(buf));
145		if (len == 0)
146			break;
147
148		written = 0;
149		while (written < len)
150			bvm_wcons(buf[written++]);
151	}
152}
153
154static void
155bvm_timeout(void *v)
156{
157	struct	tty *tp;
158	int 	c;
159
160	tp = (struct tty *)v;
161
162	tty_lock_assert(tp, MA_OWNED);
163	while ((c = bvm_cngetc(NULL)) != -1)
164		ttydisc_rint(tp, c, 0);
165	ttydisc_rint_done(tp);
166
167	callout_reset(&bvm_timer, polltime, bvm_timeout, tp);
168}
169
170static void
171bvm_cnprobe(struct consdev *cp)
172{
173	int disabled, port;
174
175	disabled = 0;
176	cp->cn_pri = CN_DEAD;
177	strcpy(cp->cn_name, "bvmcons");
178
179	resource_int_value("bvmconsole", 0, "disabled", &disabled);
180	if (!disabled) {
181		if (resource_int_value("bvmconsole", 0, "port", &port) == 0)
182			bvm_cons_port = port;
183
184		if (inw(bvm_cons_port) == BVM_CONS_SIG)
185			cp->cn_pri = CN_REMOTE;
186	}
187}
188
189static void
190bvm_cninit(struct consdev *cp)
191{
192	int i;
193	const char *bootmsg = "Using bvm console.\n";
194
195	if (boothowto & RB_VERBOSE) {
196		for (i = 0; i < strlen(bootmsg); i++)
197			bvm_cnputc(cp, bootmsg[i]);
198	}
199}
200
201static void
202bvm_cnterm(struct consdev *cp)
203{
204
205}
206
207static int
208bvm_cngetc(struct consdev *cp)
209{
210	unsigned char ch;
211
212	if (bvm_rcons(&ch) == 0) {
213#if defined(KDB)
214		kdb_alt_break(ch, &alt_break_state);
215#endif
216		return (ch);
217	}
218
219	return (-1);
220}
221
222static void
223bvm_cnputc(struct consdev *cp, int c)
224{
225
226	bvm_wcons(c);
227}
228
229static void
230bvm_cngrab(struct consdev *cp)
231{
232}
233
234static void
235bvm_cnungrab(struct consdev *cp)
236{
237}
238
239SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
240