1 /*- 2 * Copyright (C) 2011 by Nathan Whitehorn. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 17 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 19 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 20 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 21 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include <sys/cdefs.h> 26 __FBSDID("$FreeBSD$"); 27 28 #include <sys/param.h> 29 #include <sys/kdb.h> 30 #include <sys/kernel.h> 31 #include <sys/priv.h> 32 #include <sys/systm.h> 33 #include <sys/module.h> 34 #include <sys/types.h> 35 #include <sys/conf.h> 36 #include <sys/cons.h> 37 #include <sys/tty.h> 38 #include <machine/bus.h> 39 40 #include <dev/ofw/openfirm.h> 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 #include <dev/uart/uart.h> 44 #include <dev/uart/uart_cpu.h> 45 #include <dev/uart/uart_bus.h> 46 47 #include "phyp-hvcall.h" 48 #include "uart_if.h" 49 50 struct uart_phyp_softc { 51 device_t dev; 52 phandle_t node; 53 int vtermid; 54 55 struct tty *tp; 56 struct resource *irqres; 57 int irqrid; 58 struct callout callout; 59 void *sc_icookie; 60 int polltime; 61 62 struct mtx sc_mtx; 63 int protocol; 64 65 union { 66 uint64_t u64[2]; 67 char str[16]; 68 } phyp_inbuf; 69 uint64_t inbuflen; 70 uint8_t outseqno; 71 }; 72 73 static struct uart_phyp_softc *console_sc = NULL; 74 #if defined(KDB) 75 static int alt_break_state; 76 #endif 77 78 enum { 79 HVTERM1, HVTERMPROT 80 }; 81 82 #define VS_DATA_PACKET_HEADER 0xff 83 #define VS_CONTROL_PACKET_HEADER 0xfe 84 #define VSV_SET_MODEM_CTL 0x01 85 #define VSV_MODEM_CTL_UPDATE 0x02 86 #define VSV_RENEGOTIATE_CONNECTION 0x03 87 #define VS_QUERY_PACKET_HEADER 0xfd 88 #define VSV_SEND_VERSION_NUMBER 0x01 89 #define VSV_SEND_MODEM_CTL_STATUS 0x02 90 #define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc 91 92 static int uart_phyp_probe(device_t dev); 93 static int uart_phyp_attach(device_t dev); 94 static void uart_phyp_intr(void *v); 95 96 static device_method_t uart_phyp_methods[] = { 97 /* Device interface */ 98 DEVMETHOD(device_probe, uart_phyp_probe), 99 DEVMETHOD(device_attach, uart_phyp_attach), 100 101 DEVMETHOD_END 102 }; 103 104 static driver_t uart_phyp_driver = { 105 "uart", 106 uart_phyp_methods, 107 sizeof(struct uart_phyp_softc), 108 }; 109 110 DRIVER_MODULE(uart_phyp, vdevice, uart_phyp_driver, uart_devclass, 0, 0); 111 112 static cn_probe_t uart_phyp_cnprobe; 113 static cn_init_t uart_phyp_cninit; 114 static cn_term_t uart_phyp_cnterm; 115 static cn_getc_t uart_phyp_cngetc; 116 static cn_putc_t uart_phyp_cnputc; 117 static cn_grab_t uart_phyp_cngrab; 118 static cn_ungrab_t uart_phyp_cnungrab; 119 120 CONSOLE_DRIVER(uart_phyp); 121 122 static void uart_phyp_ttyoutwakeup(struct tty *tp); 123 124 static struct ttydevsw uart_phyp_tty_class = { 125 .tsw_flags = TF_INITLOCK|TF_CALLOUT, 126 .tsw_outwakeup = uart_phyp_ttyoutwakeup, 127 }; 128 129 static int 130 uart_phyp_probe_node(struct uart_phyp_softc *sc) 131 { 132 phandle_t node = sc->node; 133 uint32_t reg; 134 char buf[64]; 135 136 sc->inbuflen = 0; 137 sc->outseqno = 0; 138 139 if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0) 140 return (ENXIO); 141 if (strcmp(buf, "vty") != 0) 142 return (ENXIO); 143 144 if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0) 145 return (ENXIO); 146 if (strcmp(buf, "serial") != 0) 147 return (ENXIO); 148 149 reg = -1; 150 OF_getprop(node, "reg", ®, sizeof(reg)); 151 if (reg == -1) 152 return (ENXIO); 153 sc->vtermid = reg; 154 sc->node = node; 155 156 if (OF_getprop(node, "compatible", buf, sizeof(buf)) <= 0) 157 return (ENXIO); 158 if (strcmp(buf, "hvterm1") == 0) { 159 sc->protocol = HVTERM1; 160 return (0); 161 } else if (strcmp(buf, "hvterm-protocol") == 0) { 162 sc->protocol = HVTERMPROT; 163 return (0); 164 } 165 166 return (ENXIO); 167 } 168 169 static int 170 uart_phyp_probe(device_t dev) 171 { 172 const char *name; 173 struct uart_phyp_softc sc; 174 int err; 175 176 name = ofw_bus_get_name(dev); 177 if (name == NULL || strcmp(name, "vty") != 0) 178 return (ENXIO); 179 180 sc.node = ofw_bus_get_node(dev); 181 err = uart_phyp_probe_node(&sc); 182 if (err != 0) 183 return (err); 184 185 device_set_desc(dev, "POWER Hypervisor Virtual Serial Port"); 186 187 return (err); 188 } 189 190 static void 191 uart_phyp_cnprobe(struct consdev *cp) 192 { 193 char buf[64]; 194 ihandle_t stdout; 195 phandle_t input, opts, chosen; 196 static struct uart_phyp_softc sc; 197 198 if ((opts = OF_finddevice("/options")) == -1) 199 goto fail; 200 if ((chosen = OF_finddevice("/chosen")) == -1) 201 goto fail; 202 203 /* Check if OF has an active stdin/stdout */ 204 input = -1; 205 if (OF_getprop(chosen, "stdout", &stdout, 206 sizeof(stdout)) == sizeof(stdout) && stdout != 0) 207 input = OF_instance_to_package(stdout); 208 if (input == -1) 209 goto fail; 210 211 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 212 goto fail; 213 if (strcmp(buf, "serial") != 0) 214 goto fail; 215 216 sc.node = input; 217 if (uart_phyp_probe_node(&sc) != 0) 218 goto fail; 219 mtx_init(&sc.sc_mtx, "uart_phyp", NULL, MTX_SPIN | MTX_QUIET | 220 MTX_NOWITNESS); 221 222 cp->cn_pri = CN_NORMAL; 223 console_sc = ≻ 224 return; 225 226 fail: 227 cp->cn_pri = CN_DEAD; 228 return; 229 } 230 231 static int 232 uart_phyp_attach(device_t dev) 233 { 234 struct uart_phyp_softc *sc; 235 int unit; 236 237 sc = device_get_softc(dev); 238 sc->dev = dev; 239 sc->node = ofw_bus_get_node(dev); 240 uart_phyp_probe_node(sc); 241 242 unit = device_get_unit(dev); 243 sc->tp = tty_alloc(&uart_phyp_tty_class, sc); 244 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, 245 MTX_SPIN | MTX_QUIET | MTX_NOWITNESS); 246 247 if (console_sc != NULL && console_sc->vtermid == sc->vtermid) { 248 sc->outseqno = console_sc->outseqno; 249 console_sc = sc; 250 sprintf(uart_phyp_consdev.cn_name, "ttyu%r", unit); 251 tty_init_console(sc->tp, 0); 252 } 253 254 sc->irqrid = 0; 255 sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid, 256 RF_ACTIVE | RF_SHAREABLE); 257 if (sc->irqres != NULL) { 258 bus_setup_intr(dev, sc->irqres, INTR_TYPE_TTY | INTR_MPSAFE, 259 NULL, uart_phyp_intr, sc, &sc->sc_icookie); 260 } else { 261 callout_init(&sc->callout, CALLOUT_MPSAFE); 262 sc->polltime = hz / 20; 263 if (sc->polltime < 1) 264 sc->polltime = 1; 265 callout_reset(&sc->callout, sc->polltime, uart_phyp_intr, sc); 266 } 267 268 tty_makedev(sc->tp, NULL, "u%r", unit); 269 270 return (0); 271 } 272 273 static void 274 uart_phyp_cninit(struct consdev *cp) 275 { 276 277 strcpy(cp->cn_name, "phypcons"); 278 } 279 280 static void 281 uart_phyp_cnterm(struct consdev *cp) 282 { 283 } 284 285 static int 286 uart_phyp_get(struct uart_phyp_softc *sc, void *buffer, size_t bufsize) 287 { 288 int err; 289 290 uart_lock(&sc->sc_mtx); 291 if (sc->inbuflen == 0) { 292 err = phyp_pft_hcall(H_GET_TERM_CHAR, sc->vtermid, 293 0, 0, 0, &sc->inbuflen, &sc->phyp_inbuf.u64[0], 294 &sc->phyp_inbuf.u64[1]); 295 if (err != H_SUCCESS) { 296 uart_unlock(&sc->sc_mtx); 297 return (-1); 298 } 299 } 300 301 if (sc->inbuflen == 0) { 302 uart_unlock(&sc->sc_mtx); 303 return (0); 304 } 305 306 if (bufsize > sc->inbuflen) 307 bufsize = sc->inbuflen; 308 memcpy(buffer, sc->phyp_inbuf.str, bufsize); 309 sc->inbuflen -= bufsize; 310 if (sc->inbuflen > 0) 311 memmove(&sc->phyp_inbuf.str[0], &sc->phyp_inbuf.str[bufsize], 312 sc->inbuflen); 313 314 uart_unlock(&sc->sc_mtx); 315 return (bufsize); 316 } 317 318 static int 319 uart_phyp_put(struct uart_phyp_softc *sc, void *buffer, size_t bufsize) 320 { 321 uint16_t seqno; 322 uint64_t len = 0; 323 union { 324 uint64_t u64; 325 char bytes[8]; 326 } cbuf; 327 328 uart_lock(&sc->sc_mtx); 329 switch (sc->protocol) { 330 case HVTERM1: 331 if (bufsize > 8) 332 bufsize = 8; 333 memcpy(&cbuf, buffer, bufsize); 334 len = bufsize; 335 break; 336 case HVTERMPROT: 337 if (bufsize > 4) 338 bufsize = 4; 339 seqno = sc->outseqno++; 340 cbuf.bytes[0] = VS_DATA_PACKET_HEADER; 341 cbuf.bytes[1] = 4 + bufsize; /* total length */ 342 cbuf.bytes[2] = (seqno >> 8) & 0xff; 343 cbuf.bytes[3] = seqno & 0xff; 344 memcpy(&cbuf.bytes[4], buffer, bufsize); 345 len = 4 + bufsize; 346 break; 347 } 348 phyp_hcall(H_PUT_TERM_CHAR, sc->vtermid, len, cbuf.u64, 0); 349 uart_unlock(&sc->sc_mtx); 350 351 return (bufsize); 352 } 353 354 static int 355 uart_phyp_cngetc(struct consdev *cp) 356 { 357 unsigned char c; 358 int retval; 359 360 retval = uart_phyp_get(console_sc, &c, 1); 361 if (retval != 1) 362 return (-1); 363 #if defined(KDB) 364 kdb_alt_break(c, &alt_break_state); 365 #endif 366 367 return (c); 368 } 369 370 static void 371 uart_phyp_cnputc(struct consdev *cp, int c) 372 { 373 unsigned char ch = c; 374 uart_phyp_put(console_sc, &ch, 1); 375 } 376 377 static void 378 uart_phyp_cngrab(struct consdev *cp) 379 { 380 } 381 382 static void 383 uart_phyp_cnungrab(struct consdev *cp) 384 { 385 } 386 387 static void 388 uart_phyp_ttyoutwakeup(struct tty *tp) 389 { 390 struct uart_phyp_softc *sc; 391 char buffer[8]; 392 int len; 393 394 sc = tty_softc(tp); 395 396 while ((len = ttydisc_getc(tp, buffer, sizeof(buffer))) != 0) 397 uart_phyp_put(sc, buffer, len); 398 } 399 400 static void 401 uart_phyp_intr(void *v) 402 { 403 struct uart_phyp_softc *sc = v; 404 struct tty *tp = sc->tp; 405 unsigned char c; 406 int len; 407 408 tty_lock(tp); 409 while ((len = uart_phyp_get(sc, &c, 1)) > 0) 410 ttydisc_rint(tp, c, 0); 411 ttydisc_rint_done(tp); 412 tty_unlock(tp); 413 414 if (sc->irqres == NULL) 415 callout_reset(&sc->callout, sc->polltime, uart_phyp_intr, sc); 416 } 417 418