1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM
6 /* Host Virtual Serial Interface (HVSI) is a protocol between the hosted OS
7 * and the service processor on IBM pSeries servers. On these servers, there
8 * are no serial ports under the OS's control, and sometimes there is no other
9 * console available either. However, the service processor has two standard
10 * serial ports, so this over-complicated protocol allows the OS to control
11 * those ports by proxy.
13 * Besides data, the procotol supports the reading/writing of the serial
14 * port's DTR line, and the reading of the CD line. This is to allow the OS to
15 * control a modem attached to the service processor's serial port. Note that
16 * the OS cannot change the speed of the port through this protocol.
21 #include <linux/console.h>
22 #include <linux/ctype.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/module.h>
27 #include <linux/major.h>
28 #include <linux/kernel.h>
29 #include <linux/spinlock.h>
30 #include <linux/sysrq.h>
31 #include <linux/tty.h>
32 #include <linux/tty_flip.h>
33 #include <asm/hvcall.h>
34 #include <asm/hvconsole.h>
36 #include <linux/uaccess.h>
38 #include <asm/param.h>
41 #define HVSI_MAJOR 229
42 #define HVSI_MINOR 128
43 #define MAX_NR_HVSI_CONSOLES 4
45 #define HVSI_TIMEOUT (5*HZ)
46 #define HVSI_VERSION 1
47 #define HVSI_MAX_PACKET 256
48 #define HVSI_MAX_READ 16
49 #define HVSI_MAX_OUTGOING_DATA 12
53 * we pass data via two 8-byte registers, so we would like our char arrays
54 * properly aligned for those loads.
56 #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
60 struct delayed_work writer;
61 struct work_struct handshaker;
62 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
63 wait_queue_head_t stateq; /* woken when HVSI state changes */
66 uint8_t throttle_buf[128];
67 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
68 /* inbuf is for packet reassembly. leave a little room for leftovers. */
69 uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
75 atomic_t seqno; /* HVSI packet sequence number */
77 uint8_t state; /* HVSI protocol state */
79 #ifdef CONFIG_MAGIC_SYSRQ
81 #endif /* CONFIG_MAGIC_SYSRQ */
83 static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
85 static struct tty_driver *hvsi_driver;
86 static int hvsi_count;
87 static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
89 enum HVSI_PROTOCOL_STATE {
91 HVSI_WAIT_FOR_VER_RESPONSE,
92 HVSI_WAIT_FOR_VER_QUERY,
94 HVSI_WAIT_FOR_MCTRL_RESPONSE,
97 #define HVSI_CONSOLE 0x1
99 static inline int is_console(struct hvsi_struct *hp)
101 return hp->flags & HVSI_CONSOLE;
104 static inline int is_open(struct hvsi_struct *hp)
106 /* if we're waiting for an mctrl then we're already open */
107 return (hp->state == HVSI_OPEN)
108 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
111 static inline void print_state(struct hvsi_struct *hp)
114 static const char *state_names[] = {
116 "HVSI_WAIT_FOR_VER_RESPONSE",
117 "HVSI_WAIT_FOR_VER_QUERY",
119 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
122 const char *name = (hp->state < ARRAY_SIZE(state_names))
123 ? state_names[hp->state] : "UNKNOWN";
125 pr_debug("hvsi%i: state = %s\n", hp->index, name);
129 static inline void __set_state(struct hvsi_struct *hp, int state)
133 wake_up_all(&hp->stateq);
136 static inline void set_state(struct hvsi_struct *hp, int state)
140 spin_lock_irqsave(&hp->lock, flags);
141 __set_state(hp, state);
142 spin_unlock_irqrestore(&hp->lock, flags);
145 static inline int len_packet(const uint8_t *packet)
147 return (int)((struct hvsi_header *)packet)->len;
150 static inline int is_header(const uint8_t *packet)
152 struct hvsi_header *header = (struct hvsi_header *)packet;
153 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
156 static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
158 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
159 return 0; /* don't even have the packet header */
161 if (hp->inbuf_end < (packet + len_packet(packet)))
162 return 0; /* don't have the rest of the packet */
167 /* shift remaining bytes in packetbuf down */
168 static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
170 int remaining = (int)(hp->inbuf_end - read_to);
172 pr_debug("%s: %i chars remain\n", __func__, remaining);
174 if (read_to != hp->inbuf)
175 memmove(hp->inbuf, read_to, remaining);
177 hp->inbuf_end = hp->inbuf + remaining;
181 #define dbg_dump_packet(packet) dump_packet(packet)
182 #define dbg_dump_hex(data, len) dump_hex(data, len)
184 #define dbg_dump_packet(packet) do { } while (0)
185 #define dbg_dump_hex(data, len) do { } while (0)
188 static void dump_hex(const uint8_t *data, int len)
193 for (i=0; i < len; i++)
194 printk("%.2x", data[i]);
197 for (i=0; i < len; i++) {
198 if (isprint(data[i]))
199 printk("%c", data[i]);
206 static void dump_packet(uint8_t *packet)
208 struct hvsi_header *header = (struct hvsi_header *)packet;
210 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
213 dump_hex(packet, header->len);
216 static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
220 got = hvc_get_chars(hp->vtermno, buf, count);
225 static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
226 struct tty_struct *tty, struct hvsi_struct **to_handshake)
228 struct hvsi_control *header = (struct hvsi_control *)packet;
230 switch (be16_to_cpu(header->verb)) {
231 case VSV_MODEM_CTL_UPDATE:
232 if ((be32_to_cpu(header->word) & HVSI_TSCD) == 0) {
233 /* CD went away; no more connection */
234 pr_debug("hvsi%i: CD dropped\n", hp->index);
235 hp->mctrl &= TIOCM_CD;
236 if (tty && !C_CLOCAL(tty))
240 case VSV_CLOSE_PROTOCOL:
241 pr_debug("hvsi%i: service processor came back\n", hp->index);
242 if (hp->state != HVSI_CLOSED) {
247 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
254 static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
256 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
260 case HVSI_WAIT_FOR_VER_RESPONSE:
261 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
263 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
265 mctrl_word = be32_to_cpu(resp->u.mctrl_word);
266 if (mctrl_word & HVSI_TSDTR)
267 hp->mctrl |= TIOCM_DTR;
268 if (mctrl_word & HVSI_TSCD)
269 hp->mctrl |= TIOCM_CD;
270 __set_state(hp, HVSI_OPEN);
273 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
279 /* respond to service processor's version query */
280 static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
282 struct hvsi_query_response packet __ALIGNED__;
285 packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
286 packet.hdr.len = sizeof(struct hvsi_query_response);
287 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
288 packet.verb = cpu_to_be16(VSV_SEND_VERSION_NUMBER);
289 packet.u.version = HVSI_VERSION;
290 packet.query_seqno = cpu_to_be16(query_seqno+1);
292 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
293 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
295 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
296 if (wrote != packet.hdr.len) {
297 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
305 static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
307 struct hvsi_query *query = (struct hvsi_query *)packet;
310 case HVSI_WAIT_FOR_VER_QUERY:
311 hvsi_version_respond(hp, be16_to_cpu(query->hdr.seqno));
312 __set_state(hp, HVSI_OPEN);
315 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
321 static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
325 for (i=0; i < len; i++) {
327 #ifdef CONFIG_MAGIC_SYSRQ
331 } else if (hp->sysrq) {
336 #endif /* CONFIG_MAGIC_SYSRQ */
337 tty_insert_flip_char(&hp->port, c, 0);
342 * We could get 252 bytes of data at once here. But the tty layer only
343 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
344 * it. Accordingly we won't send more than 128 bytes at a time to the flip
345 * buffer, which will give the tty buffer a chance to throttle us. Should the
346 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
349 #define TTY_THRESHOLD_THROTTLE 128
350 static bool hvsi_recv_data(struct hvsi_struct *hp, const uint8_t *packet)
352 const struct hvsi_header *header = (const struct hvsi_header *)packet;
353 const uint8_t *data = packet + sizeof(struct hvsi_header);
354 int datalen = header->len - sizeof(struct hvsi_header);
355 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
357 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
363 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
364 datalen = TTY_THRESHOLD_THROTTLE;
367 hvsi_insert_chars(hp, data, datalen);
371 * we still have more data to deliver, so we need to save off the
372 * overflow and send it later
374 pr_debug("%s: deferring overflow\n", __func__);
375 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
376 hp->n_throttle = overflow;
383 * Returns true/false indicating data successfully read from hypervisor.
384 * Used both to get packets for tty connections and to advance the state
385 * machine during console handshaking (in which case tty = NULL and we ignore
388 static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty,
389 struct hvsi_struct **handshake)
391 uint8_t *packet = hp->inbuf;
397 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
399 pr_debug("%s: 0-length read\n", __func__);
403 pr_debug("%s: got %i bytes\n", __func__, chunklen);
404 dbg_dump_hex(hp->inbuf_end, chunklen);
406 hp->inbuf_end += chunklen;
408 /* handle all completed packets */
409 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
410 struct hvsi_header *header = (struct hvsi_header *)packet;
412 if (!is_header(packet)) {
413 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
414 /* skip bytes until we find a header or run out of data */
415 while ((packet < hp->inbuf_end) && (!is_header(packet)))
420 pr_debug("%s: handling %i-byte packet\n", __func__,
422 dbg_dump_packet(packet);
424 switch (header->type) {
425 case VS_DATA_PACKET_HEADER:
428 flip = hvsi_recv_data(hp, packet);
430 case VS_CONTROL_PACKET_HEADER:
431 hvsi_recv_control(hp, packet, tty, handshake);
433 case VS_QUERY_RESPONSE_PACKET_HEADER:
434 hvsi_recv_response(hp, packet);
436 case VS_QUERY_PACKET_HEADER:
437 hvsi_recv_query(hp, packet);
440 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
441 hp->index, header->type);
446 packet += len_packet(packet);
449 pr_debug("%s: handshake\n", __func__);
454 compact_inbuf(hp, packet);
457 tty_flip_buffer_push(&hp->port);
462 static void hvsi_send_overflow(struct hvsi_struct *hp)
464 pr_debug("%s: delivering %i bytes overflow\n", __func__,
467 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
472 * must get all pending data because we only get an irq on empty->non-empty
475 static irqreturn_t hvsi_interrupt(int irq, void *arg)
477 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
478 struct hvsi_struct *handshake;
479 struct tty_struct *tty;
483 pr_debug("%s\n", __func__);
485 tty = tty_port_tty_get(&hp->port);
488 spin_lock_irqsave(&hp->lock, flags);
489 again = hvsi_load_chunk(hp, tty, &handshake);
490 spin_unlock_irqrestore(&hp->lock, flags);
493 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
494 schedule_work(&handshake->handshaker);
498 spin_lock_irqsave(&hp->lock, flags);
499 if (tty && hp->n_throttle && !tty_throttled(tty)) {
500 /* we weren't hung up and we weren't throttled, so we can
501 * deliver the rest now */
502 hvsi_send_overflow(hp);
503 tty_flip_buffer_push(&hp->port);
505 spin_unlock_irqrestore(&hp->lock, flags);
512 /* for boot console, before the irq handler is running */
513 static int __init poll_for_state(struct hvsi_struct *hp, int state)
515 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
518 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
520 if (hp->state == state)
524 if (time_after(jiffies, end_jiffies))
529 /* wait for irq handler to change our state */
530 static int wait_for_state(struct hvsi_struct *hp, int state)
534 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
540 static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
542 struct hvsi_query packet __ALIGNED__;
545 packet.hdr.type = VS_QUERY_PACKET_HEADER;
546 packet.hdr.len = sizeof(struct hvsi_query);
547 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
548 packet.verb = cpu_to_be16(verb);
550 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
551 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
553 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
554 if (wrote != packet.hdr.len) {
555 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
563 static int hvsi_get_mctrl(struct hvsi_struct *hp)
567 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
568 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
570 ret = hvsi_wait(hp, HVSI_OPEN);
572 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
573 set_state(hp, HVSI_OPEN);
577 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
582 /* note that we can only set DTR */
583 static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
585 struct hvsi_control packet __ALIGNED__;
588 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
589 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
590 packet.hdr.len = sizeof(struct hvsi_control);
591 packet.verb = cpu_to_be16(VSV_SET_MODEM_CTL);
592 packet.mask = cpu_to_be32(HVSI_TSDTR);
594 if (mctrl & TIOCM_DTR)
595 packet.word = cpu_to_be32(HVSI_TSDTR);
597 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
598 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
600 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
601 if (wrote != packet.hdr.len) {
602 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
609 static void hvsi_drain_input(struct hvsi_struct *hp)
611 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
612 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
614 while (time_before(end_jiffies, jiffies))
615 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
619 static int hvsi_handshake(struct hvsi_struct *hp)
624 * We could have a CLOSE or other data waiting for us before we even try
625 * to open; try to throw it all away so we don't get confused. (CLOSE
626 * is the first message sent up the pipe when the FSP comes online. We
627 * need to distinguish between "it came up a while ago and we're the first
628 * user" and "it was just reset before it saw our handshake packet".)
630 hvsi_drain_input(hp);
632 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
633 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
635 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
639 ret = hvsi_wait(hp, HVSI_OPEN);
646 static void hvsi_handshaker(struct work_struct *work)
648 struct hvsi_struct *hp =
649 container_of(work, struct hvsi_struct, handshaker);
651 if (hvsi_handshake(hp) >= 0)
654 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
655 if (is_console(hp)) {
657 * ttys will re-attempt the handshake via hvsi_open, but
658 * the console will not.
660 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
664 static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
666 struct hvsi_data packet __ALIGNED__;
669 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
671 packet.hdr.type = VS_DATA_PACKET_HEADER;
672 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
673 packet.hdr.len = count + sizeof(struct hvsi_header);
674 memcpy(&packet.data, buf, count);
676 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
677 if (ret == packet.hdr.len) {
678 /* return the number of chars written, not the packet length */
681 return ret; /* return any errors */
684 static void hvsi_close_protocol(struct hvsi_struct *hp)
686 struct hvsi_control packet __ALIGNED__;
688 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
689 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
691 packet.verb = cpu_to_be16(VSV_CLOSE_PROTOCOL);
693 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
694 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
696 hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
699 static int hvsi_open(struct tty_struct *tty, struct file *filp)
701 struct hvsi_struct *hp;
705 pr_debug("%s\n", __func__);
707 hp = &hvsi_ports[tty->index];
709 tty->driver_data = hp;
712 if (hp->state == HVSI_FSP_DIED)
715 tty_port_tty_set(&hp->port, tty);
716 spin_lock_irqsave(&hp->lock, flags);
718 atomic_set(&hp->seqno, 0);
719 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
720 spin_unlock_irqrestore(&hp->lock, flags);
723 return 0; /* this has already been handshaked as the console */
725 ret = hvsi_handshake(hp);
727 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
731 ret = hvsi_get_mctrl(hp);
733 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
737 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
739 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
746 /* wait for hvsi_write_worker to empty hp->outbuf */
747 static void hvsi_flush_output(struct hvsi_struct *hp)
749 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
751 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
752 cancel_delayed_work_sync(&hp->writer);
753 flush_work(&hp->handshaker);
756 * it's also possible that our timeout expired and hvsi_write_worker
757 * didn't manage to push outbuf. poof.
762 static void hvsi_close(struct tty_struct *tty, struct file *filp)
764 struct hvsi_struct *hp = tty->driver_data;
767 pr_debug("%s\n", __func__);
769 if (tty_hung_up_p(filp))
772 spin_lock_irqsave(&hp->lock, flags);
774 if (--hp->port.count == 0) {
775 tty_port_tty_set(&hp->port, NULL);
776 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
778 /* only close down connection if it is not the console */
779 if (!is_console(hp)) {
780 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
781 __set_state(hp, HVSI_CLOSED);
783 * any data delivered to the tty layer after this will be
784 * discarded (except for XON/XOFF)
788 spin_unlock_irqrestore(&hp->lock, flags);
790 /* let any existing irq handlers finish. no more will start. */
791 synchronize_irq(hp->virq);
793 /* hvsi_write_worker will re-schedule until outbuf is empty. */
794 hvsi_flush_output(hp);
796 /* tell FSP to stop sending data */
797 hvsi_close_protocol(hp);
800 * drain anything FSP is still in the middle of sending, and let
801 * hvsi_handshake drain the rest on the next open.
803 hvsi_drain_input(hp);
805 spin_lock_irqsave(&hp->lock, flags);
807 } else if (hp->port.count < 0)
808 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
809 hp - hvsi_ports, hp->port.count);
811 spin_unlock_irqrestore(&hp->lock, flags);
814 static void hvsi_hangup(struct tty_struct *tty)
816 struct hvsi_struct *hp = tty->driver_data;
819 pr_debug("%s\n", __func__);
821 tty_port_tty_set(&hp->port, NULL);
823 spin_lock_irqsave(&hp->lock, flags);
826 spin_unlock_irqrestore(&hp->lock, flags);
829 /* called with hp->lock held */
830 static void hvsi_push(struct hvsi_struct *hp)
834 if (hp->n_outbuf <= 0)
837 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
840 pr_debug("%s: wrote %i chars\n", __func__, n);
842 } else if (n == -EIO) {
843 __set_state(hp, HVSI_FSP_DIED);
844 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
848 /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
849 static void hvsi_write_worker(struct work_struct *work)
851 struct hvsi_struct *hp =
852 container_of(work, struct hvsi_struct, writer.work);
855 static long start_j = 0;
861 spin_lock_irqsave(&hp->lock, flags);
863 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
867 * We could have a non-open connection if the service processor died
868 * while we were busily scheduling ourselves. In that case, it could
869 * be minutes before the service processor comes back, so only try
870 * again once a second.
872 schedule_delayed_work(&hp->writer, HZ);
877 if (hp->n_outbuf > 0)
878 schedule_delayed_work(&hp->writer, 10);
881 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
885 wake_up_all(&hp->emptyq);
886 tty_port_tty_wakeup(&hp->port);
890 spin_unlock_irqrestore(&hp->lock, flags);
893 static int hvsi_write_room(struct tty_struct *tty)
895 struct hvsi_struct *hp = tty->driver_data;
897 return N_OUTBUF - hp->n_outbuf;
900 static int hvsi_chars_in_buffer(struct tty_struct *tty)
902 struct hvsi_struct *hp = tty->driver_data;
907 static int hvsi_write(struct tty_struct *tty,
908 const unsigned char *buf, int count)
910 struct hvsi_struct *hp = tty->driver_data;
911 const char *source = buf;
914 int origcount = count;
916 spin_lock_irqsave(&hp->lock, flags);
918 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
921 /* we're either closing or not yet open; don't accept data */
922 pr_debug("%s: not open\n", __func__);
927 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
928 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
929 * will see there is no room in outbuf and return.
931 while ((count > 0) && (hvsi_write_room(tty) > 0)) {
932 int chunksize = min(count, hvsi_write_room(tty));
934 BUG_ON(hp->n_outbuf < 0);
935 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
936 hp->n_outbuf += chunksize;
944 if (hp->n_outbuf > 0) {
946 * we weren't able to write it all to the hypervisor.
947 * schedule another push attempt.
949 schedule_delayed_work(&hp->writer, 10);
953 spin_unlock_irqrestore(&hp->lock, flags);
955 if (total != origcount)
956 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
963 * I have never seen throttle or unthrottle called, so this little throttle
964 * buffering scheme may or may not work.
966 static void hvsi_throttle(struct tty_struct *tty)
968 struct hvsi_struct *hp = tty->driver_data;
970 pr_debug("%s\n", __func__);
972 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
975 static void hvsi_unthrottle(struct tty_struct *tty)
977 struct hvsi_struct *hp = tty->driver_data;
980 pr_debug("%s\n", __func__);
982 spin_lock_irqsave(&hp->lock, flags);
983 if (hp->n_throttle) {
984 hvsi_send_overflow(hp);
985 tty_flip_buffer_push(&hp->port);
987 spin_unlock_irqrestore(&hp->lock, flags);
990 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
993 static int hvsi_tiocmget(struct tty_struct *tty)
995 struct hvsi_struct *hp = tty->driver_data;
1001 static int hvsi_tiocmset(struct tty_struct *tty,
1002 unsigned int set, unsigned int clear)
1004 struct hvsi_struct *hp = tty->driver_data;
1005 unsigned long flags;
1008 /* we can only alter DTR */
1012 spin_lock_irqsave(&hp->lock, flags);
1014 new_mctrl = (hp->mctrl & ~clear) | set;
1016 if (hp->mctrl != new_mctrl) {
1017 hvsi_set_mctrl(hp, new_mctrl);
1018 hp->mctrl = new_mctrl;
1020 spin_unlock_irqrestore(&hp->lock, flags);
1026 static const struct tty_operations hvsi_ops = {
1028 .close = hvsi_close,
1029 .write = hvsi_write,
1030 .hangup = hvsi_hangup,
1031 .write_room = hvsi_write_room,
1032 .chars_in_buffer = hvsi_chars_in_buffer,
1033 .throttle = hvsi_throttle,
1034 .unthrottle = hvsi_unthrottle,
1035 .tiocmget = hvsi_tiocmget,
1036 .tiocmset = hvsi_tiocmset,
1039 static int __init hvsi_init(void)
1043 hvsi_driver = alloc_tty_driver(hvsi_count);
1047 hvsi_driver->driver_name = "hvsi";
1048 hvsi_driver->name = "hvsi";
1049 hvsi_driver->major = HVSI_MAJOR;
1050 hvsi_driver->minor_start = HVSI_MINOR;
1051 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1052 hvsi_driver->init_termios = tty_std_termios;
1053 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1054 hvsi_driver->init_termios.c_ispeed = 9600;
1055 hvsi_driver->init_termios.c_ospeed = 9600;
1056 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1057 tty_set_operations(hvsi_driver, &hvsi_ops);
1059 for (i=0; i < hvsi_count; i++) {
1060 struct hvsi_struct *hp = &hvsi_ports[i];
1063 tty_port_link_device(&hp->port, hvsi_driver, i);
1065 ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp);
1067 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1070 hvsi_wait = wait_for_state; /* irqs active now */
1072 ret = tty_register_driver(hvsi_driver);
1074 pr_err("Couldn't register hvsi console driver\n");
1078 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1082 hvsi_wait = poll_for_state;
1083 for (i = 0; i < hvsi_count; i++) {
1084 struct hvsi_struct *hp = &hvsi_ports[i];
1086 free_irq(hp->virq, hp);
1088 tty_driver_kref_put(hvsi_driver);
1092 device_initcall(hvsi_init);
1094 /***** console (not tty) code: *****/
1096 static void hvsi_console_print(struct console *console, const char *buf,
1099 struct hvsi_struct *hp = &hvsi_ports[console->index];
1100 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1101 unsigned int i = 0, n = 0;
1102 int ret, donecr = 0;
1109 * ugh, we have to translate LF -> CRLF ourselves, in place.
1110 * copied from hvc_console.c:
1112 while (count > 0 || i > 0) {
1113 if (count > 0 && i < sizeof(c)) {
1114 if (buf[n] == '\n' && !donecr) {
1123 ret = hvsi_put_chars(hp, c, i);
1131 static struct tty_driver *hvsi_console_device(struct console *console,
1134 *index = console->index;
1138 static int __init hvsi_console_setup(struct console *console, char *options)
1140 struct hvsi_struct *hp;
1143 if (console->index < 0 || console->index >= hvsi_count)
1145 hp = &hvsi_ports[console->index];
1147 /* give the FSP a chance to change the baud rate when we re-open */
1148 hvsi_close_protocol(hp);
1150 ret = hvsi_handshake(hp);
1154 ret = hvsi_get_mctrl(hp);
1158 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1162 hp->flags |= HVSI_CONSOLE;
1167 static struct console hvsi_console = {
1169 .write = hvsi_console_print,
1170 .device = hvsi_console_device,
1171 .setup = hvsi_console_setup,
1172 .flags = CON_PRINTBUFFER,
1176 static int __init hvsi_console_init(void)
1178 struct device_node *vty;
1180 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1182 /* search device tree for vty nodes */
1183 for_each_compatible_node(vty, "serial", "hvterm-protocol") {
1184 struct hvsi_struct *hp;
1185 const __be32 *vtermno, *irq;
1187 vtermno = of_get_property(vty, "reg", NULL);
1188 irq = of_get_property(vty, "interrupts", NULL);
1189 if (!vtermno || !irq)
1192 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1197 hp = &hvsi_ports[hvsi_count];
1198 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1199 INIT_WORK(&hp->handshaker, hvsi_handshaker);
1200 init_waitqueue_head(&hp->emptyq);
1201 init_waitqueue_head(&hp->stateq);
1202 spin_lock_init(&hp->lock);
1203 tty_port_init(&hp->port);
1204 hp->index = hvsi_count;
1205 hp->inbuf_end = hp->inbuf;
1206 hp->state = HVSI_CLOSED;
1207 hp->vtermno = be32_to_cpup(vtermno);
1208 hp->virq = irq_create_mapping(NULL, be32_to_cpup(irq));
1209 if (hp->virq == 0) {
1210 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
1211 __func__, be32_to_cpup(irq));
1212 tty_port_destroy(&hp->port);
1220 register_console(&hvsi_console);
1223 console_initcall(hvsi_console_init);