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.
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/sched.h>
43 #include <linux/spinlock.h>
44 #include <linux/sysrq.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <asm/hvcall.h>
48 #include <asm/hvconsole.h>
50 #include <asm/uaccess.h>
52 #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
66 * we pass data via two 8-byte registers, so we would like our char arrays
67 * properly aligned for those loads.
69 #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
72 struct delayed_work writer;
73 struct work_struct handshaker;
74 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
75 wait_queue_head_t stateq; /* woken when HVSI state changes */
78 struct tty_struct *tty;
80 uint8_t throttle_buf[128];
81 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
82 /* inbuf is for packet reassembly. leave a little room for leftovers. */
83 uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
89 atomic_t seqno; /* HVSI packet sequence number */
91 uint8_t state; /* HVSI protocol state */
93 #ifdef CONFIG_MAGIC_SYSRQ
95 #endif /* CONFIG_MAGIC_SYSRQ */
97 static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
99 static struct tty_driver *hvsi_driver;
100 static int hvsi_count;
101 static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
103 enum HVSI_PROTOCOL_STATE {
105 HVSI_WAIT_FOR_VER_RESPONSE,
106 HVSI_WAIT_FOR_VER_QUERY,
108 HVSI_WAIT_FOR_MCTRL_RESPONSE,
111 #define HVSI_CONSOLE 0x1
113 #define VS_DATA_PACKET_HEADER 0xff
114 #define VS_CONTROL_PACKET_HEADER 0xfe
115 #define VS_QUERY_PACKET_HEADER 0xfd
116 #define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc
119 #define VSV_SET_MODEM_CTL 1 /* to service processor only */
120 #define VSV_MODEM_CTL_UPDATE 2 /* from service processor only */
121 #define VSV_CLOSE_PROTOCOL 3
124 #define VSV_SEND_VERSION_NUMBER 1
125 #define VSV_SEND_MODEM_CTL_STATUS 2
127 /* yes, these masks are not consecutive. */
128 #define HVSI_TSDTR 0x01
129 #define HVSI_TSCD 0x20
135 } __attribute__((packed));
141 uint8_t data[HVSI_MAX_OUTGOING_DATA];
142 } __attribute__((packed));
144 struct hvsi_control {
149 /* optional depending on verb: */
152 } __attribute__((packed));
159 } __attribute__((packed));
161 struct hvsi_query_response {
166 uint16_t query_seqno;
171 } __attribute__((packed));
175 static inline int is_console(struct hvsi_struct *hp)
177 return hp->flags & HVSI_CONSOLE;
180 static inline int is_open(struct hvsi_struct *hp)
182 /* if we're waiting for an mctrl then we're already open */
183 return (hp->state == HVSI_OPEN)
184 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
187 static inline void print_state(struct hvsi_struct *hp)
190 static const char *state_names[] = {
192 "HVSI_WAIT_FOR_VER_RESPONSE",
193 "HVSI_WAIT_FOR_VER_QUERY",
195 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
198 const char *name = state_names[hp->state];
200 if (hp->state > ARRAY_SIZE(state_names))
203 pr_debug("hvsi%i: state = %s\n", hp->index, name);
207 static inline void __set_state(struct hvsi_struct *hp, int state)
211 wake_up_all(&hp->stateq);
214 static inline void set_state(struct hvsi_struct *hp, int state)
218 spin_lock_irqsave(&hp->lock, flags);
219 __set_state(hp, state);
220 spin_unlock_irqrestore(&hp->lock, flags);
223 static inline int len_packet(const uint8_t *packet)
225 return (int)((struct hvsi_header *)packet)->len;
228 static inline int is_header(const uint8_t *packet)
230 struct hvsi_header *header = (struct hvsi_header *)packet;
231 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
234 static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
236 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
237 return 0; /* don't even have the packet header */
239 if (hp->inbuf_end < (packet + len_packet(packet)))
240 return 0; /* don't have the rest of the packet */
245 /* shift remaining bytes in packetbuf down */
246 static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
248 int remaining = (int)(hp->inbuf_end - read_to);
250 pr_debug("%s: %i chars remain\n", __FUNCTION__, remaining);
252 if (read_to != hp->inbuf)
253 memmove(hp->inbuf, read_to, remaining);
255 hp->inbuf_end = hp->inbuf + remaining;
259 #define dbg_dump_packet(packet) dump_packet(packet)
260 #define dbg_dump_hex(data, len) dump_hex(data, len)
262 #define dbg_dump_packet(packet) do { } while (0)
263 #define dbg_dump_hex(data, len) do { } while (0)
266 static void dump_hex(const uint8_t *data, int len)
271 for (i=0; i < len; i++)
272 printk("%.2x", data[i]);
275 for (i=0; i < len; i++) {
276 if (isprint(data[i]))
277 printk("%c", data[i]);
284 static void dump_packet(uint8_t *packet)
286 struct hvsi_header *header = (struct hvsi_header *)packet;
288 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
291 dump_hex(packet, header->len);
294 static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
298 got = hvc_get_chars(hp->vtermno, buf, count);
303 static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
304 struct tty_struct **to_hangup, struct hvsi_struct **to_handshake)
306 struct hvsi_control *header = (struct hvsi_control *)packet;
308 switch (header->verb) {
309 case VSV_MODEM_CTL_UPDATE:
310 if ((header->word & HVSI_TSCD) == 0) {
311 /* CD went away; no more connection */
312 pr_debug("hvsi%i: CD dropped\n", hp->index);
313 hp->mctrl &= TIOCM_CD;
314 /* If userland hasn't done an open(2) yet, hp->tty is NULL. */
315 if (hp->tty && !(hp->tty->flags & CLOCAL))
316 *to_hangup = hp->tty;
319 case VSV_CLOSE_PROTOCOL:
320 pr_debug("hvsi%i: service processor came back\n", hp->index);
321 if (hp->state != HVSI_CLOSED) {
326 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
333 static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
335 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
338 case HVSI_WAIT_FOR_VER_RESPONSE:
339 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
341 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
343 if (resp->u.mctrl_word & HVSI_TSDTR)
344 hp->mctrl |= TIOCM_DTR;
345 if (resp->u.mctrl_word & HVSI_TSCD)
346 hp->mctrl |= TIOCM_CD;
347 __set_state(hp, HVSI_OPEN);
350 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
356 /* respond to service processor's version query */
357 static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
359 struct hvsi_query_response packet __ALIGNED__;
362 packet.type = VS_QUERY_RESPONSE_PACKET_HEADER;
363 packet.len = sizeof(struct hvsi_query_response);
364 packet.seqno = atomic_inc_return(&hp->seqno);
365 packet.verb = VSV_SEND_VERSION_NUMBER;
366 packet.u.version = HVSI_VERSION;
367 packet.query_seqno = query_seqno+1;
369 pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
370 dbg_dump_hex((uint8_t*)&packet, packet.len);
372 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
373 if (wrote != packet.len) {
374 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
382 static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
384 struct hvsi_query *query = (struct hvsi_query *)packet;
387 case HVSI_WAIT_FOR_VER_QUERY:
388 hvsi_version_respond(hp, query->seqno);
389 __set_state(hp, HVSI_OPEN);
392 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
398 static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
402 for (i=0; i < len; i++) {
404 #ifdef CONFIG_MAGIC_SYSRQ
408 } else if (hp->sysrq) {
409 handle_sysrq(c, hp->tty);
413 #endif /* CONFIG_MAGIC_SYSRQ */
414 tty_insert_flip_char(hp->tty, c, 0);
419 * We could get 252 bytes of data at once here. But the tty layer only
420 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
421 * it. Accordingly we won't send more than 128 bytes at a time to the flip
422 * buffer, which will give the tty buffer a chance to throttle us. Should the
423 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
426 #define TTY_THRESHOLD_THROTTLE 128
427 static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
428 const uint8_t *packet)
430 const struct hvsi_header *header = (const struct hvsi_header *)packet;
431 const uint8_t *data = packet + sizeof(struct hvsi_header);
432 int datalen = header->len - sizeof(struct hvsi_header);
433 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
435 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
441 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __FUNCTION__);
442 datalen = TTY_THRESHOLD_THROTTLE;
445 hvsi_insert_chars(hp, data, datalen);
449 * we still have more data to deliver, so we need to save off the
450 * overflow and send it later
452 pr_debug("%s: deferring overflow\n", __FUNCTION__);
453 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
454 hp->n_throttle = overflow;
461 * Returns true/false indicating data successfully read from hypervisor.
462 * Used both to get packets for tty connections and to advance the state
463 * machine during console handshaking (in which case tty = NULL and we ignore
466 static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
467 struct tty_struct **hangup, struct hvsi_struct **handshake)
469 uint8_t *packet = hp->inbuf;
476 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
478 pr_debug("%s: 0-length read\n", __FUNCTION__);
482 pr_debug("%s: got %i bytes\n", __FUNCTION__, chunklen);
483 dbg_dump_hex(hp->inbuf_end, chunklen);
485 hp->inbuf_end += chunklen;
487 /* handle all completed packets */
488 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
489 struct hvsi_header *header = (struct hvsi_header *)packet;
491 if (!is_header(packet)) {
492 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
493 /* skip bytes until we find a header or run out of data */
494 while ((packet < hp->inbuf_end) && (!is_header(packet)))
499 pr_debug("%s: handling %i-byte packet\n", __FUNCTION__,
501 dbg_dump_packet(packet);
503 switch (header->type) {
504 case VS_DATA_PACKET_HEADER:
508 break; /* no tty buffer to put data in */
509 *flip = hvsi_recv_data(hp, packet);
511 case VS_CONTROL_PACKET_HEADER:
512 hvsi_recv_control(hp, packet, hangup, handshake);
514 case VS_QUERY_RESPONSE_PACKET_HEADER:
515 hvsi_recv_response(hp, packet);
517 case VS_QUERY_PACKET_HEADER:
518 hvsi_recv_query(hp, packet);
521 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
522 hp->index, header->type);
527 packet += len_packet(packet);
529 if (*hangup || *handshake) {
530 pr_debug("%s: hangup or handshake\n", __FUNCTION__);
532 * we need to send the hangup now before receiving any more data.
533 * If we get "data, hangup, data", we can't deliver the second
534 * data before the hangup.
540 compact_inbuf(hp, packet);
545 static void hvsi_send_overflow(struct hvsi_struct *hp)
547 pr_debug("%s: delivering %i bytes overflow\n", __FUNCTION__,
550 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
555 * must get all pending data because we only get an irq on empty->non-empty
558 static irqreturn_t hvsi_interrupt(int irq, void *arg)
560 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
561 struct tty_struct *flip;
562 struct tty_struct *hangup;
563 struct hvsi_struct *handshake;
567 pr_debug("%s\n", __FUNCTION__);
570 spin_lock_irqsave(&hp->lock, flags);
571 again = hvsi_load_chunk(hp, &flip, &hangup, &handshake);
572 spin_unlock_irqrestore(&hp->lock, flags);
575 * we have to call tty_flip_buffer_push() and tty_hangup() outside our
576 * spinlock. But we also have to keep going until we've read all the
581 /* there was data put in the tty flip buffer */
582 tty_flip_buffer_push(flip);
591 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
592 schedule_work(&handshake->handshaker);
596 spin_lock_irqsave(&hp->lock, flags);
597 if (hp->tty && hp->n_throttle
598 && (!test_bit(TTY_THROTTLED, &hp->tty->flags))) {
599 /* we weren't hung up and we weren't throttled, so we can deliver the
602 hvsi_send_overflow(hp);
604 spin_unlock_irqrestore(&hp->lock, flags);
607 tty_flip_buffer_push(flip);
613 /* for boot console, before the irq handler is running */
614 static int __init poll_for_state(struct hvsi_struct *hp, int state)
616 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
619 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
621 if (hp->state == state)
625 if (time_after(jiffies, end_jiffies))
630 /* wait for irq handler to change our state */
631 static int wait_for_state(struct hvsi_struct *hp, int state)
635 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
641 static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
643 struct hvsi_query packet __ALIGNED__;
646 packet.type = VS_QUERY_PACKET_HEADER;
647 packet.len = sizeof(struct hvsi_query);
648 packet.seqno = atomic_inc_return(&hp->seqno);
651 pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
652 dbg_dump_hex((uint8_t*)&packet, packet.len);
654 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
655 if (wrote != packet.len) {
656 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
664 static int hvsi_get_mctrl(struct hvsi_struct *hp)
668 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
669 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
671 ret = hvsi_wait(hp, HVSI_OPEN);
673 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
674 set_state(hp, HVSI_OPEN);
678 pr_debug("%s: mctrl 0x%x\n", __FUNCTION__, hp->mctrl);
683 /* note that we can only set DTR */
684 static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
686 struct hvsi_control packet __ALIGNED__;
689 packet.type = VS_CONTROL_PACKET_HEADER,
690 packet.seqno = atomic_inc_return(&hp->seqno);
691 packet.len = sizeof(struct hvsi_control);
692 packet.verb = VSV_SET_MODEM_CTL;
693 packet.mask = HVSI_TSDTR;
695 if (mctrl & TIOCM_DTR)
696 packet.word = HVSI_TSDTR;
698 pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
699 dbg_dump_hex((uint8_t*)&packet, packet.len);
701 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
702 if (wrote != packet.len) {
703 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
710 static void hvsi_drain_input(struct hvsi_struct *hp)
712 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
713 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
715 while (time_before(end_jiffies, jiffies))
716 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
720 static int hvsi_handshake(struct hvsi_struct *hp)
725 * We could have a CLOSE or other data waiting for us before we even try
726 * to open; try to throw it all away so we don't get confused. (CLOSE
727 * is the first message sent up the pipe when the FSP comes online. We
728 * need to distinguish between "it came up a while ago and we're the first
729 * user" and "it was just reset before it saw our handshake packet".)
731 hvsi_drain_input(hp);
733 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
734 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
736 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
740 ret = hvsi_wait(hp, HVSI_OPEN);
747 static void hvsi_handshaker(struct work_struct *work)
749 struct hvsi_struct *hp =
750 container_of(work, struct hvsi_struct, handshaker);
752 if (hvsi_handshake(hp) >= 0)
755 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
756 if (is_console(hp)) {
758 * ttys will re-attempt the handshake via hvsi_open, but
759 * the console will not.
761 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
765 static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
767 struct hvsi_data packet __ALIGNED__;
770 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
772 packet.type = VS_DATA_PACKET_HEADER;
773 packet.seqno = atomic_inc_return(&hp->seqno);
774 packet.len = count + sizeof(struct hvsi_header);
775 memcpy(&packet.data, buf, count);
777 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
778 if (ret == packet.len) {
779 /* return the number of chars written, not the packet length */
782 return ret; /* return any errors */
785 static void hvsi_close_protocol(struct hvsi_struct *hp)
787 struct hvsi_control packet __ALIGNED__;
789 packet.type = VS_CONTROL_PACKET_HEADER;
790 packet.seqno = atomic_inc_return(&hp->seqno);
792 packet.verb = VSV_CLOSE_PROTOCOL;
794 pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
795 dbg_dump_hex((uint8_t*)&packet, packet.len);
797 hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
800 static int hvsi_open(struct tty_struct *tty, struct file *filp)
802 struct hvsi_struct *hp;
804 int line = tty->index;
807 pr_debug("%s\n", __FUNCTION__);
809 if (line < 0 || line >= hvsi_count)
811 hp = &hvsi_ports[line];
813 tty->driver_data = hp;
814 tty->low_latency = 1; /* avoid throttle/tty_flip_buffer_push race */
817 if (hp->state == HVSI_FSP_DIED)
820 spin_lock_irqsave(&hp->lock, flags);
823 atomic_set(&hp->seqno, 0);
824 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
825 spin_unlock_irqrestore(&hp->lock, flags);
828 return 0; /* this has already been handshaked as the console */
830 ret = hvsi_handshake(hp);
832 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
836 ret = hvsi_get_mctrl(hp);
838 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
842 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
844 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
851 /* wait for hvsi_write_worker to empty hp->outbuf */
852 static void hvsi_flush_output(struct hvsi_struct *hp)
854 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
856 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
857 cancel_delayed_work(&hp->writer);
858 flush_scheduled_work();
861 * it's also possible that our timeout expired and hvsi_write_worker
862 * didn't manage to push outbuf. poof.
867 static void hvsi_close(struct tty_struct *tty, struct file *filp)
869 struct hvsi_struct *hp = tty->driver_data;
872 pr_debug("%s\n", __FUNCTION__);
874 if (tty_hung_up_p(filp))
877 spin_lock_irqsave(&hp->lock, flags);
879 if (--hp->count == 0) {
881 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
883 /* only close down connection if it is not the console */
884 if (!is_console(hp)) {
885 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
886 __set_state(hp, HVSI_CLOSED);
888 * any data delivered to the tty layer after this will be
889 * discarded (except for XON/XOFF)
893 spin_unlock_irqrestore(&hp->lock, flags);
895 /* let any existing irq handlers finish. no more will start. */
896 synchronize_irq(hp->virq);
898 /* hvsi_write_worker will re-schedule until outbuf is empty. */
899 hvsi_flush_output(hp);
901 /* tell FSP to stop sending data */
902 hvsi_close_protocol(hp);
905 * drain anything FSP is still in the middle of sending, and let
906 * hvsi_handshake drain the rest on the next open.
908 hvsi_drain_input(hp);
910 spin_lock_irqsave(&hp->lock, flags);
912 } else if (hp->count < 0)
913 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
914 hp - hvsi_ports, hp->count);
916 spin_unlock_irqrestore(&hp->lock, flags);
919 static void hvsi_hangup(struct tty_struct *tty)
921 struct hvsi_struct *hp = tty->driver_data;
924 pr_debug("%s\n", __FUNCTION__);
926 spin_lock_irqsave(&hp->lock, flags);
932 spin_unlock_irqrestore(&hp->lock, flags);
935 /* called with hp->lock held */
936 static void hvsi_push(struct hvsi_struct *hp)
940 if (hp->n_outbuf <= 0)
943 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
946 pr_debug("%s: wrote %i chars\n", __FUNCTION__, n);
948 } else if (n == -EIO) {
949 __set_state(hp, HVSI_FSP_DIED);
950 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
954 /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
955 static void hvsi_write_worker(struct work_struct *work)
957 struct hvsi_struct *hp =
958 container_of(work, struct hvsi_struct, writer.work);
961 static long start_j = 0;
967 spin_lock_irqsave(&hp->lock, flags);
969 pr_debug("%s: %i chars in buffer\n", __FUNCTION__, hp->n_outbuf);
973 * We could have a non-open connection if the service processor died
974 * while we were busily scheduling ourselves. In that case, it could
975 * be minutes before the service processor comes back, so only try
976 * again once a second.
978 schedule_delayed_work(&hp->writer, HZ);
983 if (hp->n_outbuf > 0)
984 schedule_delayed_work(&hp->writer, 10);
987 pr_debug("%s: outbuf emptied after %li jiffies\n", __FUNCTION__,
991 wake_up_all(&hp->emptyq);
996 spin_unlock_irqrestore(&hp->lock, flags);
999 static int hvsi_write_room(struct tty_struct *tty)
1001 struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
1003 return N_OUTBUF - hp->n_outbuf;
1006 static int hvsi_chars_in_buffer(struct tty_struct *tty)
1008 struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
1010 return hp->n_outbuf;
1013 static int hvsi_write(struct tty_struct *tty,
1014 const unsigned char *buf, int count)
1016 struct hvsi_struct *hp = tty->driver_data;
1017 const char *source = buf;
1018 unsigned long flags;
1020 int origcount = count;
1022 spin_lock_irqsave(&hp->lock, flags);
1024 pr_debug("%s: %i chars in buffer\n", __FUNCTION__, hp->n_outbuf);
1027 /* we're either closing or not yet open; don't accept data */
1028 pr_debug("%s: not open\n", __FUNCTION__);
1033 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
1034 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
1035 * will see there is no room in outbuf and return.
1037 while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) {
1038 int chunksize = min(count, hvsi_write_room(hp->tty));
1040 BUG_ON(hp->n_outbuf < 0);
1041 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
1042 hp->n_outbuf += chunksize;
1045 source += chunksize;
1050 if (hp->n_outbuf > 0) {
1052 * we weren't able to write it all to the hypervisor.
1053 * schedule another push attempt.
1055 schedule_delayed_work(&hp->writer, 10);
1059 spin_unlock_irqrestore(&hp->lock, flags);
1061 if (total != origcount)
1062 pr_debug("%s: wanted %i, only wrote %i\n", __FUNCTION__, origcount,
1069 * I have never seen throttle or unthrottle called, so this little throttle
1070 * buffering scheme may or may not work.
1072 static void hvsi_throttle(struct tty_struct *tty)
1074 struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
1076 pr_debug("%s\n", __FUNCTION__);
1078 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
1081 static void hvsi_unthrottle(struct tty_struct *tty)
1083 struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
1084 unsigned long flags;
1087 pr_debug("%s\n", __FUNCTION__);
1089 spin_lock_irqsave(&hp->lock, flags);
1090 if (hp->n_throttle) {
1091 hvsi_send_overflow(hp);
1094 spin_unlock_irqrestore(&hp->lock, flags);
1097 tty_flip_buffer_push(hp->tty);
1099 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1102 static int hvsi_tiocmget(struct tty_struct *tty, struct file *file)
1104 struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
1110 static int hvsi_tiocmset(struct tty_struct *tty, struct file *file,
1111 unsigned int set, unsigned int clear)
1113 struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
1114 unsigned long flags;
1117 /* we can only alter DTR */
1121 spin_lock_irqsave(&hp->lock, flags);
1123 new_mctrl = (hp->mctrl & ~clear) | set;
1125 if (hp->mctrl != new_mctrl) {
1126 hvsi_set_mctrl(hp, new_mctrl);
1127 hp->mctrl = new_mctrl;
1129 spin_unlock_irqrestore(&hp->lock, flags);
1135 static const struct tty_operations hvsi_ops = {
1137 .close = hvsi_close,
1138 .write = hvsi_write,
1139 .hangup = hvsi_hangup,
1140 .write_room = hvsi_write_room,
1141 .chars_in_buffer = hvsi_chars_in_buffer,
1142 .throttle = hvsi_throttle,
1143 .unthrottle = hvsi_unthrottle,
1144 .tiocmget = hvsi_tiocmget,
1145 .tiocmset = hvsi_tiocmset,
1148 static int __init hvsi_init(void)
1152 hvsi_driver = alloc_tty_driver(hvsi_count);
1156 hvsi_driver->owner = THIS_MODULE;
1157 hvsi_driver->driver_name = "hvsi";
1158 hvsi_driver->name = "hvsi";
1159 hvsi_driver->major = HVSI_MAJOR;
1160 hvsi_driver->minor_start = HVSI_MINOR;
1161 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1162 hvsi_driver->init_termios = tty_std_termios;
1163 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1164 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1165 tty_set_operations(hvsi_driver, &hvsi_ops);
1167 for (i=0; i < hvsi_count; i++) {
1168 struct hvsi_struct *hp = &hvsi_ports[i];
1171 ret = request_irq(hp->virq, hvsi_interrupt, IRQF_DISABLED, "hvsi", hp);
1173 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1176 hvsi_wait = wait_for_state; /* irqs active now */
1178 if (tty_register_driver(hvsi_driver))
1179 panic("Couldn't register hvsi console driver\n");
1181 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1185 device_initcall(hvsi_init);
1187 /***** console (not tty) code: *****/
1189 static void hvsi_console_print(struct console *console, const char *buf,
1192 struct hvsi_struct *hp = &hvsi_ports[console->index];
1193 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1194 unsigned int i = 0, n = 0;
1195 int ret, donecr = 0;
1202 * ugh, we have to translate LF -> CRLF ourselves, in place.
1203 * copied from hvc_console.c:
1205 while (count > 0 || i > 0) {
1206 if (count > 0 && i < sizeof(c)) {
1207 if (buf[n] == '\n' && !donecr) {
1216 ret = hvsi_put_chars(hp, c, i);
1224 static struct tty_driver *hvsi_console_device(struct console *console,
1227 *index = console->index;
1231 static int __init hvsi_console_setup(struct console *console, char *options)
1233 struct hvsi_struct *hp = &hvsi_ports[console->index];
1236 if (console->index < 0 || console->index >= hvsi_count)
1239 /* give the FSP a chance to change the baud rate when we re-open */
1240 hvsi_close_protocol(hp);
1242 ret = hvsi_handshake(hp);
1246 ret = hvsi_get_mctrl(hp);
1250 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1254 hp->flags |= HVSI_CONSOLE;
1259 static struct console hvsi_con_driver = {
1261 .write = hvsi_console_print,
1262 .device = hvsi_console_device,
1263 .setup = hvsi_console_setup,
1264 .flags = CON_PRINTBUFFER,
1268 static int __init hvsi_console_init(void)
1270 struct device_node *vty;
1272 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1274 /* search device tree for vty nodes */
1275 for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol");
1277 vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) {
1278 struct hvsi_struct *hp;
1279 const uint32_t *vtermno, *irq;
1281 vtermno = get_property(vty, "reg", NULL);
1282 irq = get_property(vty, "interrupts", NULL);
1283 if (!vtermno || !irq)
1286 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1291 hp = &hvsi_ports[hvsi_count];
1292 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1293 INIT_WORK(&hp->handshaker, hvsi_handshaker);
1294 init_waitqueue_head(&hp->emptyq);
1295 init_waitqueue_head(&hp->stateq);
1296 spin_lock_init(&hp->lock);
1297 hp->index = hvsi_count;
1298 hp->inbuf_end = hp->inbuf;
1299 hp->state = HVSI_CLOSED;
1300 hp->vtermno = *vtermno;
1301 hp->virq = irq_create_mapping(NULL, irq[0]);
1302 if (hp->virq == NO_IRQ) {
1303 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
1304 __FUNCTION__, irq[0]);
1312 register_console(&hvsi_con_driver);
1315 console_initcall(hvsi_console_init);