console: add /proc/consoles
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / hvsi.c
bloba2bc885ce60a8e2bab93a815ab6ffc7d86640220
1 /*
2 * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /* Host Virtual Serial Interface (HVSI) is a protocol between the hosted OS
20 * and the service processor on IBM pSeries servers. On these servers, there
21 * are no serial ports under the OS's control, and sometimes there is no other
22 * console available either. However, the service processor has two standard
23 * serial ports, so this over-complicated protocol allows the OS to control
24 * those ports by proxy.
26 * Besides data, the procotol supports the reading/writing of the serial
27 * port's DTR line, and the reading of the CD line. This is to allow the OS to
28 * control a modem attached to the service processor's serial port. Note that
29 * the OS cannot change the speed of the port through this protocol.
32 #undef DEBUG
34 #include <linux/console.h>
35 #include <linux/ctype.h>
36 #include <linux/delay.h>
37 #include <linux/init.h>
38 #include <linux/interrupt.h>
39 #include <linux/module.h>
40 #include <linux/major.h>
41 #include <linux/kernel.h>
42 #include <linux/spinlock.h>
43 #include <linux/sysrq.h>
44 #include <linux/tty.h>
45 #include <linux/tty_flip.h>
46 #include <asm/hvcall.h>
47 #include <asm/hvconsole.h>
48 #include <asm/prom.h>
49 #include <asm/uaccess.h>
50 #include <asm/vio.h>
51 #include <asm/param.h>
53 #define HVSI_MAJOR 229
54 #define HVSI_MINOR 128
55 #define MAX_NR_HVSI_CONSOLES 4
57 #define HVSI_TIMEOUT (5*HZ)
58 #define HVSI_VERSION 1
59 #define HVSI_MAX_PACKET 256
60 #define HVSI_MAX_READ 16
61 #define HVSI_MAX_OUTGOING_DATA 12
62 #define N_OUTBUF 12
65 * we pass data via two 8-byte registers, so we would like our char arrays
66 * properly aligned for those loads.
68 #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
70 struct hvsi_struct {
71 struct delayed_work writer;
72 struct work_struct handshaker;
73 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
74 wait_queue_head_t stateq; /* woken when HVSI state changes */
75 spinlock_t lock;
76 int index;
77 struct tty_struct *tty;
78 int count;
79 uint8_t throttle_buf[128];
80 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
81 /* inbuf is for packet reassembly. leave a little room for leftovers. */
82 uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
83 uint8_t *inbuf_end;
84 int n_throttle;
85 int n_outbuf;
86 uint32_t vtermno;
87 uint32_t virq;
88 atomic_t seqno; /* HVSI packet sequence number */
89 uint16_t mctrl;
90 uint8_t state; /* HVSI protocol state */
91 uint8_t flags;
92 #ifdef CONFIG_MAGIC_SYSRQ
93 uint8_t sysrq;
94 #endif /* CONFIG_MAGIC_SYSRQ */
96 static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
98 static struct tty_driver *hvsi_driver;
99 static int hvsi_count;
100 static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
102 enum HVSI_PROTOCOL_STATE {
103 HVSI_CLOSED,
104 HVSI_WAIT_FOR_VER_RESPONSE,
105 HVSI_WAIT_FOR_VER_QUERY,
106 HVSI_OPEN,
107 HVSI_WAIT_FOR_MCTRL_RESPONSE,
108 HVSI_FSP_DIED,
110 #define HVSI_CONSOLE 0x1
112 #define VS_DATA_PACKET_HEADER 0xff
113 #define VS_CONTROL_PACKET_HEADER 0xfe
114 #define VS_QUERY_PACKET_HEADER 0xfd
115 #define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc
117 /* control verbs */
118 #define VSV_SET_MODEM_CTL 1 /* to service processor only */
119 #define VSV_MODEM_CTL_UPDATE 2 /* from service processor only */
120 #define VSV_CLOSE_PROTOCOL 3
122 /* query verbs */
123 #define VSV_SEND_VERSION_NUMBER 1
124 #define VSV_SEND_MODEM_CTL_STATUS 2
126 /* yes, these masks are not consecutive. */
127 #define HVSI_TSDTR 0x01
128 #define HVSI_TSCD 0x20
130 struct hvsi_header {
131 uint8_t type;
132 uint8_t len;
133 uint16_t seqno;
134 } __attribute__((packed));
136 struct hvsi_data {
137 uint8_t type;
138 uint8_t len;
139 uint16_t seqno;
140 uint8_t data[HVSI_MAX_OUTGOING_DATA];
141 } __attribute__((packed));
143 struct hvsi_control {
144 uint8_t type;
145 uint8_t len;
146 uint16_t seqno;
147 uint16_t verb;
148 /* optional depending on verb: */
149 uint32_t word;
150 uint32_t mask;
151 } __attribute__((packed));
153 struct hvsi_query {
154 uint8_t type;
155 uint8_t len;
156 uint16_t seqno;
157 uint16_t verb;
158 } __attribute__((packed));
160 struct hvsi_query_response {
161 uint8_t type;
162 uint8_t len;
163 uint16_t seqno;
164 uint16_t verb;
165 uint16_t query_seqno;
166 union {
167 uint8_t version;
168 uint32_t mctrl_word;
169 } u;
170 } __attribute__((packed));
174 static inline int is_console(struct hvsi_struct *hp)
176 return hp->flags & HVSI_CONSOLE;
179 static inline int is_open(struct hvsi_struct *hp)
181 /* if we're waiting for an mctrl then we're already open */
182 return (hp->state == HVSI_OPEN)
183 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
186 static inline void print_state(struct hvsi_struct *hp)
188 #ifdef DEBUG
189 static const char *state_names[] = {
190 "HVSI_CLOSED",
191 "HVSI_WAIT_FOR_VER_RESPONSE",
192 "HVSI_WAIT_FOR_VER_QUERY",
193 "HVSI_OPEN",
194 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
195 "HVSI_FSP_DIED",
197 const char *name = (hp->state < ARRAY_SIZE(state_names))
198 ? state_names[hp->state] : "UNKNOWN";
200 pr_debug("hvsi%i: state = %s\n", hp->index, name);
201 #endif /* DEBUG */
204 static inline void __set_state(struct hvsi_struct *hp, int state)
206 hp->state = state;
207 print_state(hp);
208 wake_up_all(&hp->stateq);
211 static inline void set_state(struct hvsi_struct *hp, int state)
213 unsigned long flags;
215 spin_lock_irqsave(&hp->lock, flags);
216 __set_state(hp, state);
217 spin_unlock_irqrestore(&hp->lock, flags);
220 static inline int len_packet(const uint8_t *packet)
222 return (int)((struct hvsi_header *)packet)->len;
225 static inline int is_header(const uint8_t *packet)
227 struct hvsi_header *header = (struct hvsi_header *)packet;
228 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
231 static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
233 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
234 return 0; /* don't even have the packet header */
236 if (hp->inbuf_end < (packet + len_packet(packet)))
237 return 0; /* don't have the rest of the packet */
239 return 1;
242 /* shift remaining bytes in packetbuf down */
243 static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
245 int remaining = (int)(hp->inbuf_end - read_to);
247 pr_debug("%s: %i chars remain\n", __func__, remaining);
249 if (read_to != hp->inbuf)
250 memmove(hp->inbuf, read_to, remaining);
252 hp->inbuf_end = hp->inbuf + remaining;
255 #ifdef DEBUG
256 #define dbg_dump_packet(packet) dump_packet(packet)
257 #define dbg_dump_hex(data, len) dump_hex(data, len)
258 #else
259 #define dbg_dump_packet(packet) do { } while (0)
260 #define dbg_dump_hex(data, len) do { } while (0)
261 #endif
263 static void dump_hex(const uint8_t *data, int len)
265 int i;
267 printk(" ");
268 for (i=0; i < len; i++)
269 printk("%.2x", data[i]);
271 printk("\n ");
272 for (i=0; i < len; i++) {
273 if (isprint(data[i]))
274 printk("%c", data[i]);
275 else
276 printk(".");
278 printk("\n");
281 static void dump_packet(uint8_t *packet)
283 struct hvsi_header *header = (struct hvsi_header *)packet;
285 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
286 header->seqno);
288 dump_hex(packet, header->len);
291 static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
293 unsigned long got;
295 got = hvc_get_chars(hp->vtermno, buf, count);
297 return got;
300 static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
301 struct tty_struct **to_hangup, struct hvsi_struct **to_handshake)
303 struct hvsi_control *header = (struct hvsi_control *)packet;
305 switch (header->verb) {
306 case VSV_MODEM_CTL_UPDATE:
307 if ((header->word & HVSI_TSCD) == 0) {
308 /* CD went away; no more connection */
309 pr_debug("hvsi%i: CD dropped\n", hp->index);
310 hp->mctrl &= TIOCM_CD;
311 /* If userland hasn't done an open(2) yet, hp->tty is NULL. */
312 if (hp->tty && !(hp->tty->flags & CLOCAL))
313 *to_hangup = hp->tty;
315 break;
316 case VSV_CLOSE_PROTOCOL:
317 pr_debug("hvsi%i: service processor came back\n", hp->index);
318 if (hp->state != HVSI_CLOSED) {
319 *to_handshake = hp;
321 break;
322 default:
323 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
324 hp->index);
325 dump_packet(packet);
326 break;
330 static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
332 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
334 switch (hp->state) {
335 case HVSI_WAIT_FOR_VER_RESPONSE:
336 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
337 break;
338 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
339 hp->mctrl = 0;
340 if (resp->u.mctrl_word & HVSI_TSDTR)
341 hp->mctrl |= TIOCM_DTR;
342 if (resp->u.mctrl_word & HVSI_TSCD)
343 hp->mctrl |= TIOCM_CD;
344 __set_state(hp, HVSI_OPEN);
345 break;
346 default:
347 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
348 dump_packet(packet);
349 break;
353 /* respond to service processor's version query */
354 static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
356 struct hvsi_query_response packet __ALIGNED__;
357 int wrote;
359 packet.type = VS_QUERY_RESPONSE_PACKET_HEADER;
360 packet.len = sizeof(struct hvsi_query_response);
361 packet.seqno = atomic_inc_return(&hp->seqno);
362 packet.verb = VSV_SEND_VERSION_NUMBER;
363 packet.u.version = HVSI_VERSION;
364 packet.query_seqno = query_seqno+1;
366 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
367 dbg_dump_hex((uint8_t*)&packet, packet.len);
369 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
370 if (wrote != packet.len) {
371 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
372 hp->index);
373 return -EIO;
376 return 0;
379 static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
381 struct hvsi_query *query = (struct hvsi_query *)packet;
383 switch (hp->state) {
384 case HVSI_WAIT_FOR_VER_QUERY:
385 hvsi_version_respond(hp, query->seqno);
386 __set_state(hp, HVSI_OPEN);
387 break;
388 default:
389 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
390 dump_packet(packet);
391 break;
395 static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
397 int i;
399 for (i=0; i < len; i++) {
400 char c = buf[i];
401 #ifdef CONFIG_MAGIC_SYSRQ
402 if (c == '\0') {
403 hp->sysrq = 1;
404 continue;
405 } else if (hp->sysrq) {
406 handle_sysrq(c);
407 hp->sysrq = 0;
408 continue;
410 #endif /* CONFIG_MAGIC_SYSRQ */
411 tty_insert_flip_char(hp->tty, c, 0);
416 * We could get 252 bytes of data at once here. But the tty layer only
417 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
418 * it. Accordingly we won't send more than 128 bytes at a time to the flip
419 * buffer, which will give the tty buffer a chance to throttle us. Should the
420 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
421 * revisited.
423 #define TTY_THRESHOLD_THROTTLE 128
424 static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
425 const uint8_t *packet)
427 const struct hvsi_header *header = (const struct hvsi_header *)packet;
428 const uint8_t *data = packet + sizeof(struct hvsi_header);
429 int datalen = header->len - sizeof(struct hvsi_header);
430 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
432 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
434 if (datalen == 0)
435 return NULL;
437 if (overflow > 0) {
438 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
439 datalen = TTY_THRESHOLD_THROTTLE;
442 hvsi_insert_chars(hp, data, datalen);
444 if (overflow > 0) {
446 * we still have more data to deliver, so we need to save off the
447 * overflow and send it later
449 pr_debug("%s: deferring overflow\n", __func__);
450 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
451 hp->n_throttle = overflow;
454 return hp->tty;
458 * Returns true/false indicating data successfully read from hypervisor.
459 * Used both to get packets for tty connections and to advance the state
460 * machine during console handshaking (in which case tty = NULL and we ignore
461 * incoming data).
463 static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
464 struct tty_struct **hangup, struct hvsi_struct **handshake)
466 uint8_t *packet = hp->inbuf;
467 int chunklen;
469 *flip = NULL;
470 *hangup = NULL;
471 *handshake = NULL;
473 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
474 if (chunklen == 0) {
475 pr_debug("%s: 0-length read\n", __func__);
476 return 0;
479 pr_debug("%s: got %i bytes\n", __func__, chunklen);
480 dbg_dump_hex(hp->inbuf_end, chunklen);
482 hp->inbuf_end += chunklen;
484 /* handle all completed packets */
485 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
486 struct hvsi_header *header = (struct hvsi_header *)packet;
488 if (!is_header(packet)) {
489 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
490 /* skip bytes until we find a header or run out of data */
491 while ((packet < hp->inbuf_end) && (!is_header(packet)))
492 packet++;
493 continue;
496 pr_debug("%s: handling %i-byte packet\n", __func__,
497 len_packet(packet));
498 dbg_dump_packet(packet);
500 switch (header->type) {
501 case VS_DATA_PACKET_HEADER:
502 if (!is_open(hp))
503 break;
504 if (hp->tty == NULL)
505 break; /* no tty buffer to put data in */
506 *flip = hvsi_recv_data(hp, packet);
507 break;
508 case VS_CONTROL_PACKET_HEADER:
509 hvsi_recv_control(hp, packet, hangup, handshake);
510 break;
511 case VS_QUERY_RESPONSE_PACKET_HEADER:
512 hvsi_recv_response(hp, packet);
513 break;
514 case VS_QUERY_PACKET_HEADER:
515 hvsi_recv_query(hp, packet);
516 break;
517 default:
518 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
519 hp->index, header->type);
520 dump_packet(packet);
521 break;
524 packet += len_packet(packet);
526 if (*hangup || *handshake) {
527 pr_debug("%s: hangup or handshake\n", __func__);
529 * we need to send the hangup now before receiving any more data.
530 * If we get "data, hangup, data", we can't deliver the second
531 * data before the hangup.
533 break;
537 compact_inbuf(hp, packet);
539 return 1;
542 static void hvsi_send_overflow(struct hvsi_struct *hp)
544 pr_debug("%s: delivering %i bytes overflow\n", __func__,
545 hp->n_throttle);
547 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
548 hp->n_throttle = 0;
552 * must get all pending data because we only get an irq on empty->non-empty
553 * transition
555 static irqreturn_t hvsi_interrupt(int irq, void *arg)
557 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
558 struct tty_struct *flip;
559 struct tty_struct *hangup;
560 struct hvsi_struct *handshake;
561 unsigned long flags;
562 int again = 1;
564 pr_debug("%s\n", __func__);
566 while (again) {
567 spin_lock_irqsave(&hp->lock, flags);
568 again = hvsi_load_chunk(hp, &flip, &hangup, &handshake);
569 spin_unlock_irqrestore(&hp->lock, flags);
572 * we have to call tty_flip_buffer_push() and tty_hangup() outside our
573 * spinlock. But we also have to keep going until we've read all the
574 * available data.
577 if (flip) {
578 /* there was data put in the tty flip buffer */
579 tty_flip_buffer_push(flip);
580 flip = NULL;
583 if (hangup) {
584 tty_hangup(hangup);
587 if (handshake) {
588 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
589 schedule_work(&handshake->handshaker);
593 spin_lock_irqsave(&hp->lock, flags);
594 if (hp->tty && hp->n_throttle
595 && (!test_bit(TTY_THROTTLED, &hp->tty->flags))) {
596 /* we weren't hung up and we weren't throttled, so we can deliver the
597 * rest now */
598 flip = hp->tty;
599 hvsi_send_overflow(hp);
601 spin_unlock_irqrestore(&hp->lock, flags);
603 if (flip) {
604 tty_flip_buffer_push(flip);
607 return IRQ_HANDLED;
610 /* for boot console, before the irq handler is running */
611 static int __init poll_for_state(struct hvsi_struct *hp, int state)
613 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
615 for (;;) {
616 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
618 if (hp->state == state)
619 return 0;
621 mdelay(5);
622 if (time_after(jiffies, end_jiffies))
623 return -EIO;
627 /* wait for irq handler to change our state */
628 static int wait_for_state(struct hvsi_struct *hp, int state)
630 int ret = 0;
632 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
633 ret = -EIO;
635 return ret;
638 static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
640 struct hvsi_query packet __ALIGNED__;
641 int wrote;
643 packet.type = VS_QUERY_PACKET_HEADER;
644 packet.len = sizeof(struct hvsi_query);
645 packet.seqno = atomic_inc_return(&hp->seqno);
646 packet.verb = verb;
648 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
649 dbg_dump_hex((uint8_t*)&packet, packet.len);
651 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
652 if (wrote != packet.len) {
653 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
654 wrote);
655 return -EIO;
658 return 0;
661 static int hvsi_get_mctrl(struct hvsi_struct *hp)
663 int ret;
665 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
666 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
668 ret = hvsi_wait(hp, HVSI_OPEN);
669 if (ret < 0) {
670 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
671 set_state(hp, HVSI_OPEN);
672 return ret;
675 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
677 return 0;
680 /* note that we can only set DTR */
681 static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
683 struct hvsi_control packet __ALIGNED__;
684 int wrote;
686 packet.type = VS_CONTROL_PACKET_HEADER,
687 packet.seqno = atomic_inc_return(&hp->seqno);
688 packet.len = sizeof(struct hvsi_control);
689 packet.verb = VSV_SET_MODEM_CTL;
690 packet.mask = HVSI_TSDTR;
692 if (mctrl & TIOCM_DTR)
693 packet.word = HVSI_TSDTR;
695 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
696 dbg_dump_hex((uint8_t*)&packet, packet.len);
698 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
699 if (wrote != packet.len) {
700 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
701 return -EIO;
704 return 0;
707 static void hvsi_drain_input(struct hvsi_struct *hp)
709 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
710 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
712 while (time_before(end_jiffies, jiffies))
713 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
714 break;
717 static int hvsi_handshake(struct hvsi_struct *hp)
719 int ret;
722 * We could have a CLOSE or other data waiting for us before we even try
723 * to open; try to throw it all away so we don't get confused. (CLOSE
724 * is the first message sent up the pipe when the FSP comes online. We
725 * need to distinguish between "it came up a while ago and we're the first
726 * user" and "it was just reset before it saw our handshake packet".)
728 hvsi_drain_input(hp);
730 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
731 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
732 if (ret < 0) {
733 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
734 return ret;
737 ret = hvsi_wait(hp, HVSI_OPEN);
738 if (ret < 0)
739 return ret;
741 return 0;
744 static void hvsi_handshaker(struct work_struct *work)
746 struct hvsi_struct *hp =
747 container_of(work, struct hvsi_struct, handshaker);
749 if (hvsi_handshake(hp) >= 0)
750 return;
752 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
753 if (is_console(hp)) {
755 * ttys will re-attempt the handshake via hvsi_open, but
756 * the console will not.
758 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
762 static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
764 struct hvsi_data packet __ALIGNED__;
765 int ret;
767 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
769 packet.type = VS_DATA_PACKET_HEADER;
770 packet.seqno = atomic_inc_return(&hp->seqno);
771 packet.len = count + sizeof(struct hvsi_header);
772 memcpy(&packet.data, buf, count);
774 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
775 if (ret == packet.len) {
776 /* return the number of chars written, not the packet length */
777 return count;
779 return ret; /* return any errors */
782 static void hvsi_close_protocol(struct hvsi_struct *hp)
784 struct hvsi_control packet __ALIGNED__;
786 packet.type = VS_CONTROL_PACKET_HEADER;
787 packet.seqno = atomic_inc_return(&hp->seqno);
788 packet.len = 6;
789 packet.verb = VSV_CLOSE_PROTOCOL;
791 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
792 dbg_dump_hex((uint8_t*)&packet, packet.len);
794 hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
797 static int hvsi_open(struct tty_struct *tty, struct file *filp)
799 struct hvsi_struct *hp;
800 unsigned long flags;
801 int line = tty->index;
802 int ret;
804 pr_debug("%s\n", __func__);
806 if (line < 0 || line >= hvsi_count)
807 return -ENODEV;
808 hp = &hvsi_ports[line];
810 tty->driver_data = hp;
812 mb();
813 if (hp->state == HVSI_FSP_DIED)
814 return -EIO;
816 spin_lock_irqsave(&hp->lock, flags);
817 hp->tty = tty;
818 hp->count++;
819 atomic_set(&hp->seqno, 0);
820 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
821 spin_unlock_irqrestore(&hp->lock, flags);
823 if (is_console(hp))
824 return 0; /* this has already been handshaked as the console */
826 ret = hvsi_handshake(hp);
827 if (ret < 0) {
828 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
829 return ret;
832 ret = hvsi_get_mctrl(hp);
833 if (ret < 0) {
834 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
835 return ret;
838 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
839 if (ret < 0) {
840 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
841 return ret;
844 return 0;
847 /* wait for hvsi_write_worker to empty hp->outbuf */
848 static void hvsi_flush_output(struct hvsi_struct *hp)
850 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
852 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
853 cancel_delayed_work(&hp->writer);
854 flush_scheduled_work();
857 * it's also possible that our timeout expired and hvsi_write_worker
858 * didn't manage to push outbuf. poof.
860 hp->n_outbuf = 0;
863 static void hvsi_close(struct tty_struct *tty, struct file *filp)
865 struct hvsi_struct *hp = tty->driver_data;
866 unsigned long flags;
868 pr_debug("%s\n", __func__);
870 if (tty_hung_up_p(filp))
871 return;
873 spin_lock_irqsave(&hp->lock, flags);
875 if (--hp->count == 0) {
876 hp->tty = NULL;
877 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
879 /* only close down connection if it is not the console */
880 if (!is_console(hp)) {
881 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
882 __set_state(hp, HVSI_CLOSED);
884 * any data delivered to the tty layer after this will be
885 * discarded (except for XON/XOFF)
887 tty->closing = 1;
889 spin_unlock_irqrestore(&hp->lock, flags);
891 /* let any existing irq handlers finish. no more will start. */
892 synchronize_irq(hp->virq);
894 /* hvsi_write_worker will re-schedule until outbuf is empty. */
895 hvsi_flush_output(hp);
897 /* tell FSP to stop sending data */
898 hvsi_close_protocol(hp);
901 * drain anything FSP is still in the middle of sending, and let
902 * hvsi_handshake drain the rest on the next open.
904 hvsi_drain_input(hp);
906 spin_lock_irqsave(&hp->lock, flags);
908 } else if (hp->count < 0)
909 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
910 hp - hvsi_ports, hp->count);
912 spin_unlock_irqrestore(&hp->lock, flags);
915 static void hvsi_hangup(struct tty_struct *tty)
917 struct hvsi_struct *hp = tty->driver_data;
918 unsigned long flags;
920 pr_debug("%s\n", __func__);
922 spin_lock_irqsave(&hp->lock, flags);
924 hp->count = 0;
925 hp->n_outbuf = 0;
926 hp->tty = NULL;
928 spin_unlock_irqrestore(&hp->lock, flags);
931 /* called with hp->lock held */
932 static void hvsi_push(struct hvsi_struct *hp)
934 int n;
936 if (hp->n_outbuf <= 0)
937 return;
939 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
940 if (n > 0) {
941 /* success */
942 pr_debug("%s: wrote %i chars\n", __func__, n);
943 hp->n_outbuf = 0;
944 } else if (n == -EIO) {
945 __set_state(hp, HVSI_FSP_DIED);
946 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
950 /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
951 static void hvsi_write_worker(struct work_struct *work)
953 struct hvsi_struct *hp =
954 container_of(work, struct hvsi_struct, writer.work);
955 unsigned long flags;
956 #ifdef DEBUG
957 static long start_j = 0;
959 if (start_j == 0)
960 start_j = jiffies;
961 #endif /* DEBUG */
963 spin_lock_irqsave(&hp->lock, flags);
965 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
967 if (!is_open(hp)) {
969 * We could have a non-open connection if the service processor died
970 * while we were busily scheduling ourselves. In that case, it could
971 * be minutes before the service processor comes back, so only try
972 * again once a second.
974 schedule_delayed_work(&hp->writer, HZ);
975 goto out;
978 hvsi_push(hp);
979 if (hp->n_outbuf > 0)
980 schedule_delayed_work(&hp->writer, 10);
981 else {
982 #ifdef DEBUG
983 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
984 jiffies - start_j);
985 start_j = 0;
986 #endif /* DEBUG */
987 wake_up_all(&hp->emptyq);
988 tty_wakeup(hp->tty);
991 out:
992 spin_unlock_irqrestore(&hp->lock, flags);
995 static int hvsi_write_room(struct tty_struct *tty)
997 struct hvsi_struct *hp = tty->driver_data;
999 return N_OUTBUF - hp->n_outbuf;
1002 static int hvsi_chars_in_buffer(struct tty_struct *tty)
1004 struct hvsi_struct *hp = tty->driver_data;
1006 return hp->n_outbuf;
1009 static int hvsi_write(struct tty_struct *tty,
1010 const unsigned char *buf, int count)
1012 struct hvsi_struct *hp = tty->driver_data;
1013 const char *source = buf;
1014 unsigned long flags;
1015 int total = 0;
1016 int origcount = count;
1018 spin_lock_irqsave(&hp->lock, flags);
1020 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
1022 if (!is_open(hp)) {
1023 /* we're either closing or not yet open; don't accept data */
1024 pr_debug("%s: not open\n", __func__);
1025 goto out;
1029 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
1030 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
1031 * will see there is no room in outbuf and return.
1033 while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) {
1034 int chunksize = min(count, hvsi_write_room(hp->tty));
1036 BUG_ON(hp->n_outbuf < 0);
1037 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
1038 hp->n_outbuf += chunksize;
1040 total += chunksize;
1041 source += chunksize;
1042 count -= chunksize;
1043 hvsi_push(hp);
1046 if (hp->n_outbuf > 0) {
1048 * we weren't able to write it all to the hypervisor.
1049 * schedule another push attempt.
1051 schedule_delayed_work(&hp->writer, 10);
1054 out:
1055 spin_unlock_irqrestore(&hp->lock, flags);
1057 if (total != origcount)
1058 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
1059 total);
1061 return total;
1065 * I have never seen throttle or unthrottle called, so this little throttle
1066 * buffering scheme may or may not work.
1068 static void hvsi_throttle(struct tty_struct *tty)
1070 struct hvsi_struct *hp = tty->driver_data;
1072 pr_debug("%s\n", __func__);
1074 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
1077 static void hvsi_unthrottle(struct tty_struct *tty)
1079 struct hvsi_struct *hp = tty->driver_data;
1080 unsigned long flags;
1081 int shouldflip = 0;
1083 pr_debug("%s\n", __func__);
1085 spin_lock_irqsave(&hp->lock, flags);
1086 if (hp->n_throttle) {
1087 hvsi_send_overflow(hp);
1088 shouldflip = 1;
1090 spin_unlock_irqrestore(&hp->lock, flags);
1092 if (shouldflip)
1093 tty_flip_buffer_push(hp->tty);
1095 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1098 static int hvsi_tiocmget(struct tty_struct *tty, struct file *file)
1100 struct hvsi_struct *hp = tty->driver_data;
1102 hvsi_get_mctrl(hp);
1103 return hp->mctrl;
1106 static int hvsi_tiocmset(struct tty_struct *tty, struct file *file,
1107 unsigned int set, unsigned int clear)
1109 struct hvsi_struct *hp = tty->driver_data;
1110 unsigned long flags;
1111 uint16_t new_mctrl;
1113 /* we can only alter DTR */
1114 clear &= TIOCM_DTR;
1115 set &= TIOCM_DTR;
1117 spin_lock_irqsave(&hp->lock, flags);
1119 new_mctrl = (hp->mctrl & ~clear) | set;
1121 if (hp->mctrl != new_mctrl) {
1122 hvsi_set_mctrl(hp, new_mctrl);
1123 hp->mctrl = new_mctrl;
1125 spin_unlock_irqrestore(&hp->lock, flags);
1127 return 0;
1131 static const struct tty_operations hvsi_ops = {
1132 .open = hvsi_open,
1133 .close = hvsi_close,
1134 .write = hvsi_write,
1135 .hangup = hvsi_hangup,
1136 .write_room = hvsi_write_room,
1137 .chars_in_buffer = hvsi_chars_in_buffer,
1138 .throttle = hvsi_throttle,
1139 .unthrottle = hvsi_unthrottle,
1140 .tiocmget = hvsi_tiocmget,
1141 .tiocmset = hvsi_tiocmset,
1144 static int __init hvsi_init(void)
1146 int i;
1148 hvsi_driver = alloc_tty_driver(hvsi_count);
1149 if (!hvsi_driver)
1150 return -ENOMEM;
1152 hvsi_driver->owner = THIS_MODULE;
1153 hvsi_driver->driver_name = "hvsi";
1154 hvsi_driver->name = "hvsi";
1155 hvsi_driver->major = HVSI_MAJOR;
1156 hvsi_driver->minor_start = HVSI_MINOR;
1157 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1158 hvsi_driver->init_termios = tty_std_termios;
1159 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1160 hvsi_driver->init_termios.c_ispeed = 9600;
1161 hvsi_driver->init_termios.c_ospeed = 9600;
1162 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1163 tty_set_operations(hvsi_driver, &hvsi_ops);
1165 for (i=0; i < hvsi_count; i++) {
1166 struct hvsi_struct *hp = &hvsi_ports[i];
1167 int ret = 1;
1169 ret = request_irq(hp->virq, hvsi_interrupt, IRQF_DISABLED, "hvsi", hp);
1170 if (ret)
1171 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1172 hp->virq, ret);
1174 hvsi_wait = wait_for_state; /* irqs active now */
1176 if (tty_register_driver(hvsi_driver))
1177 panic("Couldn't register hvsi console driver\n");
1179 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1181 return 0;
1183 device_initcall(hvsi_init);
1185 /***** console (not tty) code: *****/
1187 static void hvsi_console_print(struct console *console, const char *buf,
1188 unsigned int count)
1190 struct hvsi_struct *hp = &hvsi_ports[console->index];
1191 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1192 unsigned int i = 0, n = 0;
1193 int ret, donecr = 0;
1195 mb();
1196 if (!is_open(hp))
1197 return;
1200 * ugh, we have to translate LF -> CRLF ourselves, in place.
1201 * copied from hvc_console.c:
1203 while (count > 0 || i > 0) {
1204 if (count > 0 && i < sizeof(c)) {
1205 if (buf[n] == '\n' && !donecr) {
1206 c[i++] = '\r';
1207 donecr = 1;
1208 } else {
1209 c[i++] = buf[n++];
1210 donecr = 0;
1211 --count;
1213 } else {
1214 ret = hvsi_put_chars(hp, c, i);
1215 if (ret < 0)
1216 i = 0;
1217 i -= ret;
1222 static struct tty_driver *hvsi_console_device(struct console *console,
1223 int *index)
1225 *index = console->index;
1226 return hvsi_driver;
1229 static int __init hvsi_console_setup(struct console *console, char *options)
1231 struct hvsi_struct *hp;
1232 int ret;
1234 if (console->index < 0 || console->index >= hvsi_count)
1235 return -1;
1236 hp = &hvsi_ports[console->index];
1238 /* give the FSP a chance to change the baud rate when we re-open */
1239 hvsi_close_protocol(hp);
1241 ret = hvsi_handshake(hp);
1242 if (ret < 0)
1243 return ret;
1245 ret = hvsi_get_mctrl(hp);
1246 if (ret < 0)
1247 return ret;
1249 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1250 if (ret < 0)
1251 return ret;
1253 hp->flags |= HVSI_CONSOLE;
1255 return 0;
1258 static struct console hvsi_console = {
1259 .name = "hvsi",
1260 .write = hvsi_console_print,
1261 .device = hvsi_console_device,
1262 .setup = hvsi_console_setup,
1263 .flags = CON_PRINTBUFFER,
1264 .index = -1,
1267 static int __init hvsi_console_init(void)
1269 struct device_node *vty;
1271 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1273 /* search device tree for vty nodes */
1274 for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol");
1275 vty != NULL;
1276 vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) {
1277 struct hvsi_struct *hp;
1278 const uint32_t *vtermno, *irq;
1280 vtermno = of_get_property(vty, "reg", NULL);
1281 irq = of_get_property(vty, "interrupts", NULL);
1282 if (!vtermno || !irq)
1283 continue;
1285 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1286 of_node_put(vty);
1287 break;
1290 hp = &hvsi_ports[hvsi_count];
1291 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1292 INIT_WORK(&hp->handshaker, hvsi_handshaker);
1293 init_waitqueue_head(&hp->emptyq);
1294 init_waitqueue_head(&hp->stateq);
1295 spin_lock_init(&hp->lock);
1296 hp->index = hvsi_count;
1297 hp->inbuf_end = hp->inbuf;
1298 hp->state = HVSI_CLOSED;
1299 hp->vtermno = *vtermno;
1300 hp->virq = irq_create_mapping(NULL, irq[0]);
1301 if (hp->virq == NO_IRQ) {
1302 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
1303 __func__, irq[0]);
1304 continue;
1307 hvsi_count++;
1310 if (hvsi_count)
1311 register_console(&hvsi_console);
1312 return 0;
1314 console_initcall(hvsi_console_init);