35 #include <linux/ctype.h>
39 #include <linux/module.h>
41 #include <linux/kernel.h>
44 #include <linux/tty.h>
49 #include <asm/uaccess.h>
51 #include <asm/param.h>
54 #define HVSI_MAJOR 229
55 #define HVSI_MINOR 128
56 #define MAX_NR_HVSI_CONSOLES 4
58 #define HVSI_TIMEOUT (5*HZ)
59 #define HVSI_VERSION 1
60 #define HVSI_MAX_PACKET 256
61 #define HVSI_MAX_READ 16
62 #define HVSI_MAX_OUTGOING_DATA 12
69 #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
92 #ifdef CONFIG_MAGIC_SYSRQ
99 static int hvsi_count;
110 #define HVSI_CONSOLE 0x1
112 static inline int is_console(
struct hvsi_struct *hp)
124 static inline void print_state(
struct hvsi_struct *hp)
127 static const char *state_names[] = {
129 "HVSI_WAIT_FOR_VER_RESPONSE",
130 "HVSI_WAIT_FOR_VER_QUERY",
132 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
136 ? state_names[hp->
state] :
"UNKNOWN";
154 __set_state(hp, state);
155 spin_unlock_irqrestore(&hp->
lock, flags);
163 static inline int is_header(
const uint8_t *packet)
174 if (hp->
inbuf_end < (packet + len_packet(packet)))
185 pr_debug(
"%s: %i chars remain\n", __func__, remaining);
187 if (read_to != hp->
inbuf)
194 #define dbg_dump_packet(packet) dump_packet(packet)
195 #define dbg_dump_hex(data, len) dump_hex(data, len)
197 #define dbg_dump_packet(packet) do { } while (0)
198 #define dbg_dump_hex(data, len) do { } while (0)
206 for (i=0; i <
len; i++)
210 for (i=0; i <
len; i++) {
219 static void dump_packet(
uint8_t *packet)
223 printk(
"type 0x%x, len %i, seqno %i:\n", header->
type, header->
len,
226 dump_hex(packet, header->
len);
243 switch (header->
verb) {
254 pr_debug(
"hvsi%i: service processor came back\n", hp->
index);
301 packet.query_seqno = query_seqno+1;
303 pr_debug(
"%s: sending %i bytes\n", __func__, packet.hdr.len);
307 if (wrote != packet.hdr.len) {
322 hvsi_version_respond(hp, query->
hdr.seqno);
333 const char *
buf,
int len)
337 for (i=0; i < len; i++) {
339 #ifdef CONFIG_MAGIC_SYSRQ
343 }
else if (hp->sysrq) {
349 tty_insert_flip_char(tty, c, 0);
361 #define TTY_THRESHOLD_THROTTLE 128
370 pr_debug(
"queueing %i chars '%.*s'\n", datalen, datalen, data);
376 pr_debug(
"%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
380 hvsi_insert_chars(hp, tty, data, datalen);
387 pr_debug(
"%s: deferring overflow\n", __func__);
412 pr_debug(
"%s: 0-length read\n", __func__);
416 pr_debug(
"%s: got %i bytes\n", __func__, chunklen);
425 if (!is_header(packet)) {
433 pr_debug(
"%s: handling %i-byte packet\n", __func__,
437 switch (header->
type) {
443 flip = hvsi_recv_data(hp, tty, packet);
446 hvsi_recv_control(hp, packet, tty, handshake);
449 hvsi_recv_response(hp, packet);
452 hvsi_recv_query(hp, packet);
461 packet += len_packet(packet);
464 pr_debug(
"%s: handshake\n", __func__);
469 compact_inbuf(hp, packet);
479 pr_debug(
"%s: delivering %i bytes overflow\n", __func__,
504 again = hvsi_load_chunk(hp, tty, &handshake);
505 spin_unlock_irqrestore(&hp->
lock, flags);
508 pr_debug(
"hvsi%i: attempting re-handshake\n", handshake->
index);
517 hvsi_send_overflow(hp, tty);
520 spin_unlock_irqrestore(&hp->
lock, flags);
533 hvsi_interrupt(hp->
virq, (
void *)hp);
535 if (hp->
state == state)
545 static int wait_for_state(
struct hvsi_struct *hp,
int state)
565 pr_debug(
"%s: sending %i bytes\n", __func__, packet.hdr.len);
569 if (wrote != packet.hdr.len) {
612 pr_debug(
"%s: sending %i bytes\n", __func__, packet.hdr.len);
616 if (wrote != packet.hdr.len) {
624 static void hvsi_drain_input(
struct hvsi_struct *hp)
645 hvsi_drain_input(hp);
666 if (hvsi_handshake(hp) >= 0)
670 if (is_console(hp)) {
679 static int hvsi_put_chars(
struct hvsi_struct *hp,
const char *buf,
int count)
688 packet.hdr.len = count +
sizeof(
struct hvsi_header);
689 memcpy(&packet.data, buf, count);
692 if (ret == packet.hdr.len) {
699 static void hvsi_close_protocol(
struct hvsi_struct *hp)
708 pr_debug(
"%s: sending %i bytes\n", __func__, packet.hdr.len);
722 hp = &hvsi_ports[tty->
index];
734 h_vio_signal(hp->
vtermno, VIO_IRQ_ENABLE);
735 spin_unlock_irqrestore(&hp->
lock, flags);
740 ret = hvsi_handshake(hp);
746 ret = hvsi_get_mctrl(hp);
752 ret = hvsi_set_mctrl(hp, hp->
mctrl | TIOCM_DTR);
762 static void hvsi_flush_output(
struct hvsi_struct *hp)
789 if (--hp->
port.count == 0) {
794 if (!is_console(hp)) {
795 h_vio_signal(hp->
vtermno, VIO_IRQ_DISABLE);
803 spin_unlock_irqrestore(&hp->
lock, flags);
809 hvsi_flush_output(hp);
812 hvsi_close_protocol(hp);
818 hvsi_drain_input(hp);
822 }
else if (hp->
port.count < 0)
824 hp - hvsi_ports, hp->
port.count);
826 spin_unlock_irqrestore(&hp->
lock, flags);
829 static void hvsi_hangup(
struct tty_struct *tty)
841 spin_unlock_irqrestore(&hp->
lock, flags);
855 pr_debug(
"%s: wrote %i chars\n", __func__, n);
857 }
else if (n == -
EIO) {
864 static void hvsi_write_worker(
struct work_struct *work)
871 static long start_j = 0;
897 pr_debug(
"%s: outbuf emptied after %li jiffies\n", __func__,
910 spin_unlock_irqrestore(&hp->
lock, flags);
913 static int hvsi_write_room(
struct tty_struct *tty)
920 static int hvsi_chars_in_buffer(
struct tty_struct *tty)
928 const unsigned char *buf,
int count)
934 int origcount =
count;
942 pr_debug(
"%s: not open\n", __func__);
951 while ((count > 0) && (hvsi_write_room(tty) > 0)) {
952 int chunksize =
min(count, hvsi_write_room(tty));
973 spin_unlock_irqrestore(&hp->
lock, flags);
975 if (total != origcount)
976 pr_debug(
"%s: wanted %i, only wrote %i\n", __func__, origcount,
986 static void hvsi_throttle(
struct tty_struct *tty)
992 h_vio_signal(hp->
vtermno, VIO_IRQ_DISABLE);
995 static void hvsi_unthrottle(
struct tty_struct *tty)
1004 hvsi_send_overflow(hp, tty);
1007 spin_unlock_irqrestore(&hp->
lock, flags);
1010 h_vio_signal(hp->
vtermno, VIO_IRQ_ENABLE);
1013 static int hvsi_tiocmget(
struct tty_struct *tty)
1021 static int hvsi_tiocmset(
struct tty_struct *tty,
1022 unsigned int set,
unsigned int clear)
1025 unsigned long flags;
1034 new_mctrl = (hp->
mctrl & ~clear) |
set;
1036 if (hp->
mctrl != new_mctrl) {
1037 hvsi_set_mctrl(hp, new_mctrl);
1038 hp->
mctrl = new_mctrl;
1040 spin_unlock_irqrestore(&hp->
lock, flags);
1048 .close = hvsi_close,
1049 .write = hvsi_write,
1050 .hangup = hvsi_hangup,
1051 .write_room = hvsi_write_room,
1052 .chars_in_buffer = hvsi_chars_in_buffer,
1053 .throttle = hvsi_throttle,
1054 .unthrottle = hvsi_unthrottle,
1055 .tiocmget = hvsi_tiocmget,
1056 .tiocmset = hvsi_tiocmset,
1059 static int __init hvsi_init(
void)
1063 hvsi_driver = alloc_tty_driver(hvsi_count);
1068 hvsi_driver->
name =
"hvsi";
1079 for (i=0; i < hvsi_count; i++) {
1090 hvsi_wait = wait_for_state;
1093 panic(
"Couldn't register hvsi console driver\n");
1103 static void hvsi_console_print(
struct console *
console,
const char *buf,
1108 unsigned int i = 0, n = 0;
1109 int ret, donecr = 0;
1119 while (count > 0 || i > 0) {
1120 if (count > 0 && i <
sizeof(c)) {
1121 if (buf[n] ==
'\n' && !donecr) {
1130 ret = hvsi_put_chars(hp, c, i);
1138 static struct tty_driver *hvsi_console_device(
struct console *console,
1141 *index = console->
index;
1145 static int __init hvsi_console_setup(
struct console *console,
char *
options)
1150 if (console->
index < 0 || console->
index >= hvsi_count)
1152 hp = &hvsi_ports[console->
index];
1155 hvsi_close_protocol(hp);
1157 ret = hvsi_handshake(hp);
1161 ret = hvsi_get_mctrl(hp);
1165 ret = hvsi_set_mctrl(hp, hp->
mctrl | TIOCM_DTR);
1174 static struct console hvsi_console = {
1176 .write = hvsi_console_print,
1177 .device = hvsi_console_device,
1178 .setup = hvsi_console_setup,
1183 static int __init hvsi_console_init(
void)
1187 hvsi_wait = poll_for_state;
1198 if (!vtermno || !irq)
1206 hp = &hvsi_ports[hvsi_count];
1213 hp->
index = hvsi_count;
1218 if (hp->
virq == 0) {