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/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>
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
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))))
73 struct delayed_work writer;
74 struct work_struct handshaker;
75 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
76 wait_queue_head_t stateq; /* woken when HVSI state changes */
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];
88 atomic_t seqno; /* HVSI packet sequence number */
90 uint8_t state; /* HVSI protocol state */
92 #ifdef CONFIG_MAGIC_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 {
104 HVSI_WAIT_FOR_VER_RESPONSE,
105 HVSI_WAIT_FOR_VER_QUERY,
107 HVSI_WAIT_FOR_MCTRL_RESPONSE,
110 #define HVSI_CONSOLE 0x1
112 static inline int is_console(struct hvsi_struct *hp)
114 return hp->flags & HVSI_CONSOLE;
117 static inline int is_open(struct hvsi_struct *hp)
119 /* if we're waiting for an mctrl then we're already open */
120 return (hp->state == HVSI_OPEN)
121 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
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",
135 const char *name = (hp->state < ARRAY_SIZE(state_names))
136 ? state_names[hp->state] : "UNKNOWN";
138 pr_debug("hvsi%i: state = %s\n", hp->index, name);
142 static inline void __set_state(struct hvsi_struct *hp, int state)
146 wake_up_all(&hp->stateq);
149 static inline void set_state(struct hvsi_struct *hp, int state)
153 spin_lock_irqsave(&hp->lock, flags);
154 __set_state(hp, state);
155 spin_unlock_irqrestore(&hp->lock, flags);
158 static inline int len_packet(const uint8_t *packet)
160 return (int)((struct hvsi_header *)packet)->len;
163 static inline int is_header(const uint8_t *packet)
165 struct hvsi_header *header = (struct hvsi_header *)packet;
166 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
169 static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
171 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
172 return 0; /* don't even have the packet header */
174 if (hp->inbuf_end < (packet + len_packet(packet)))
175 return 0; /* don't have the rest of the packet */
180 /* shift remaining bytes in packetbuf down */
181 static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
183 int remaining = (int)(hp->inbuf_end - read_to);
185 pr_debug("%s: %i chars remain\n", __func__, remaining);
187 if (read_to != hp->inbuf)
188 memmove(hp->inbuf, read_to, remaining);
190 hp->inbuf_end = hp->inbuf + remaining;
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)
201 static void dump_hex(const uint8_t *data, int len)
206 for (i=0; i < len; i++)
207 printk("%.2x", data[i]);
210 for (i=0; i < len; i++) {
211 if (isprint(data[i]))
212 printk("%c", data[i]);
219 static void dump_packet(uint8_t *packet)
221 struct hvsi_header *header = (struct hvsi_header *)packet;
223 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
226 dump_hex(packet, header->len);
229 static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
233 got = hvc_get_chars(hp->vtermno, buf, count);
238 static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
239 struct tty_struct *tty, struct hvsi_struct **to_handshake)
241 struct hvsi_control *header = (struct hvsi_control *)packet;
243 switch (header->verb) {
244 case VSV_MODEM_CTL_UPDATE:
245 if ((header->word & HVSI_TSCD) == 0) {
246 /* CD went away; no more connection */
247 pr_debug("hvsi%i: CD dropped\n", hp->index);
248 hp->mctrl &= TIOCM_CD;
249 if (tty && !C_CLOCAL(tty))
253 case VSV_CLOSE_PROTOCOL:
254 pr_debug("hvsi%i: service processor came back\n", hp->index);
255 if (hp->state != HVSI_CLOSED) {
260 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
267 static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
269 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
272 case HVSI_WAIT_FOR_VER_RESPONSE:
273 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
275 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
277 if (resp->u.mctrl_word & HVSI_TSDTR)
278 hp->mctrl |= TIOCM_DTR;
279 if (resp->u.mctrl_word & HVSI_TSCD)
280 hp->mctrl |= TIOCM_CD;
281 __set_state(hp, HVSI_OPEN);
284 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
290 /* respond to service processor's version query */
291 static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
293 struct hvsi_query_response packet __ALIGNED__;
296 packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
297 packet.hdr.len = sizeof(struct hvsi_query_response);
298 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
299 packet.verb = VSV_SEND_VERSION_NUMBER;
300 packet.u.version = HVSI_VERSION;
301 packet.query_seqno = query_seqno+1;
303 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
304 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
306 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
307 if (wrote != packet.hdr.len) {
308 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
316 static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
318 struct hvsi_query *query = (struct hvsi_query *)packet;
321 case HVSI_WAIT_FOR_VER_QUERY:
322 hvsi_version_respond(hp, query->hdr.seqno);
323 __set_state(hp, HVSI_OPEN);
326 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
332 static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
336 for (i=0; i < len; i++) {
338 #ifdef CONFIG_MAGIC_SYSRQ
342 } else if (hp->sysrq) {
347 #endif /* CONFIG_MAGIC_SYSRQ */
348 tty_insert_flip_char(&hp->port, c, 0);
353 * We could get 252 bytes of data at once here. But the tty layer only
354 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
355 * it. Accordingly we won't send more than 128 bytes at a time to the flip
356 * buffer, which will give the tty buffer a chance to throttle us. Should the
357 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
360 #define TTY_THRESHOLD_THROTTLE 128
361 static bool hvsi_recv_data(struct hvsi_struct *hp, const uint8_t *packet)
363 const struct hvsi_header *header = (const struct hvsi_header *)packet;
364 const uint8_t *data = packet + sizeof(struct hvsi_header);
365 int datalen = header->len - sizeof(struct hvsi_header);
366 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
368 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
374 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
375 datalen = TTY_THRESHOLD_THROTTLE;
378 hvsi_insert_chars(hp, data, datalen);
382 * we still have more data to deliver, so we need to save off the
383 * overflow and send it later
385 pr_debug("%s: deferring overflow\n", __func__);
386 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
387 hp->n_throttle = overflow;
394 * Returns true/false indicating data successfully read from hypervisor.
395 * Used both to get packets for tty connections and to advance the state
396 * machine during console handshaking (in which case tty = NULL and we ignore
399 static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty,
400 struct hvsi_struct **handshake)
402 uint8_t *packet = hp->inbuf;
408 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
410 pr_debug("%s: 0-length read\n", __func__);
414 pr_debug("%s: got %i bytes\n", __func__, chunklen);
415 dbg_dump_hex(hp->inbuf_end, chunklen);
417 hp->inbuf_end += chunklen;
419 /* handle all completed packets */
420 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
421 struct hvsi_header *header = (struct hvsi_header *)packet;
423 if (!is_header(packet)) {
424 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
425 /* skip bytes until we find a header or run out of data */
426 while ((packet < hp->inbuf_end) && (!is_header(packet)))
431 pr_debug("%s: handling %i-byte packet\n", __func__,
433 dbg_dump_packet(packet);
435 switch (header->type) {
436 case VS_DATA_PACKET_HEADER:
439 flip = hvsi_recv_data(hp, packet);
441 case VS_CONTROL_PACKET_HEADER:
442 hvsi_recv_control(hp, packet, tty, handshake);
444 case VS_QUERY_RESPONSE_PACKET_HEADER:
445 hvsi_recv_response(hp, packet);
447 case VS_QUERY_PACKET_HEADER:
448 hvsi_recv_query(hp, packet);
451 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
452 hp->index, header->type);
457 packet += len_packet(packet);
460 pr_debug("%s: handshake\n", __func__);
465 compact_inbuf(hp, packet);
468 tty_flip_buffer_push(&hp->port);
473 static void hvsi_send_overflow(struct hvsi_struct *hp)
475 pr_debug("%s: delivering %i bytes overflow\n", __func__,
478 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
483 * must get all pending data because we only get an irq on empty->non-empty
486 static irqreturn_t hvsi_interrupt(int irq, void *arg)
488 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
489 struct hvsi_struct *handshake;
490 struct tty_struct *tty;
494 pr_debug("%s\n", __func__);
496 tty = tty_port_tty_get(&hp->port);
499 spin_lock_irqsave(&hp->lock, flags);
500 again = hvsi_load_chunk(hp, tty, &handshake);
501 spin_unlock_irqrestore(&hp->lock, flags);
504 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
505 schedule_work(&handshake->handshaker);
509 spin_lock_irqsave(&hp->lock, flags);
510 if (tty && hp->n_throttle && !test_bit(TTY_THROTTLED, &tty->flags)) {
511 /* we weren't hung up and we weren't throttled, so we can
512 * deliver the rest now */
513 hvsi_send_overflow(hp);
514 tty_flip_buffer_push(&hp->port);
516 spin_unlock_irqrestore(&hp->lock, flags);
523 /* for boot console, before the irq handler is running */
524 static int __init poll_for_state(struct hvsi_struct *hp, int state)
526 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
529 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
531 if (hp->state == state)
535 if (time_after(jiffies, end_jiffies))
540 /* wait for irq handler to change our state */
541 static int wait_for_state(struct hvsi_struct *hp, int state)
545 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
551 static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
553 struct hvsi_query packet __ALIGNED__;
556 packet.hdr.type = VS_QUERY_PACKET_HEADER;
557 packet.hdr.len = sizeof(struct hvsi_query);
558 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
561 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
562 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
564 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
565 if (wrote != packet.hdr.len) {
566 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
574 static int hvsi_get_mctrl(struct hvsi_struct *hp)
578 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
579 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
581 ret = hvsi_wait(hp, HVSI_OPEN);
583 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
584 set_state(hp, HVSI_OPEN);
588 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
593 /* note that we can only set DTR */
594 static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
596 struct hvsi_control packet __ALIGNED__;
599 packet.hdr.type = VS_CONTROL_PACKET_HEADER,
600 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
601 packet.hdr.len = sizeof(struct hvsi_control);
602 packet.verb = VSV_SET_MODEM_CTL;
603 packet.mask = HVSI_TSDTR;
605 if (mctrl & TIOCM_DTR)
606 packet.word = HVSI_TSDTR;
608 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
609 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
611 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
612 if (wrote != packet.hdr.len) {
613 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
620 static void hvsi_drain_input(struct hvsi_struct *hp)
622 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
623 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
625 while (time_before(end_jiffies, jiffies))
626 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
630 static int hvsi_handshake(struct hvsi_struct *hp)
635 * We could have a CLOSE or other data waiting for us before we even try
636 * to open; try to throw it all away so we don't get confused. (CLOSE
637 * is the first message sent up the pipe when the FSP comes online. We
638 * need to distinguish between "it came up a while ago and we're the first
639 * user" and "it was just reset before it saw our handshake packet".)
641 hvsi_drain_input(hp);
643 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
644 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
646 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
650 ret = hvsi_wait(hp, HVSI_OPEN);
657 static void hvsi_handshaker(struct work_struct *work)
659 struct hvsi_struct *hp =
660 container_of(work, struct hvsi_struct, handshaker);
662 if (hvsi_handshake(hp) >= 0)
665 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
666 if (is_console(hp)) {
668 * ttys will re-attempt the handshake via hvsi_open, but
669 * the console will not.
671 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
675 static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
677 struct hvsi_data packet __ALIGNED__;
680 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
682 packet.hdr.type = VS_DATA_PACKET_HEADER;
683 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
684 packet.hdr.len = count + sizeof(struct hvsi_header);
685 memcpy(&packet.data, buf, count);
687 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
688 if (ret == packet.hdr.len) {
689 /* return the number of chars written, not the packet length */
692 return ret; /* return any errors */
695 static void hvsi_close_protocol(struct hvsi_struct *hp)
697 struct hvsi_control packet __ALIGNED__;
699 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
700 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
702 packet.verb = VSV_CLOSE_PROTOCOL;
704 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
705 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
707 hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
710 static int hvsi_open(struct tty_struct *tty, struct file *filp)
712 struct hvsi_struct *hp;
716 pr_debug("%s\n", __func__);
718 hp = &hvsi_ports[tty->index];
720 tty->driver_data = hp;
723 if (hp->state == HVSI_FSP_DIED)
726 tty_port_tty_set(&hp->port, tty);
727 spin_lock_irqsave(&hp->lock, flags);
729 atomic_set(&hp->seqno, 0);
730 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
731 spin_unlock_irqrestore(&hp->lock, flags);
734 return 0; /* this has already been handshaked as the console */
736 ret = hvsi_handshake(hp);
738 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
742 ret = hvsi_get_mctrl(hp);
744 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
748 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
750 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
757 /* wait for hvsi_write_worker to empty hp->outbuf */
758 static void hvsi_flush_output(struct hvsi_struct *hp)
760 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
762 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
763 cancel_delayed_work_sync(&hp->writer);
764 flush_work(&hp->handshaker);
767 * it's also possible that our timeout expired and hvsi_write_worker
768 * didn't manage to push outbuf. poof.
773 static void hvsi_close(struct tty_struct *tty, struct file *filp)
775 struct hvsi_struct *hp = tty->driver_data;
778 pr_debug("%s\n", __func__);
780 if (tty_hung_up_p(filp))
783 spin_lock_irqsave(&hp->lock, flags);
785 if (--hp->port.count == 0) {
786 tty_port_tty_set(&hp->port, NULL);
787 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
789 /* only close down connection if it is not the console */
790 if (!is_console(hp)) {
791 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
792 __set_state(hp, HVSI_CLOSED);
794 * any data delivered to the tty layer after this will be
795 * discarded (except for XON/XOFF)
799 spin_unlock_irqrestore(&hp->lock, flags);
801 /* let any existing irq handlers finish. no more will start. */
802 synchronize_irq(hp->virq);
804 /* hvsi_write_worker will re-schedule until outbuf is empty. */
805 hvsi_flush_output(hp);
807 /* tell FSP to stop sending data */
808 hvsi_close_protocol(hp);
811 * drain anything FSP is still in the middle of sending, and let
812 * hvsi_handshake drain the rest on the next open.
814 hvsi_drain_input(hp);
816 spin_lock_irqsave(&hp->lock, flags);
818 } else if (hp->port.count < 0)
819 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
820 hp - hvsi_ports, hp->port.count);
822 spin_unlock_irqrestore(&hp->lock, flags);
825 static void hvsi_hangup(struct tty_struct *tty)
827 struct hvsi_struct *hp = tty->driver_data;
830 pr_debug("%s\n", __func__);
832 tty_port_tty_set(&hp->port, NULL);
834 spin_lock_irqsave(&hp->lock, flags);
837 spin_unlock_irqrestore(&hp->lock, flags);
840 /* called with hp->lock held */
841 static void hvsi_push(struct hvsi_struct *hp)
845 if (hp->n_outbuf <= 0)
848 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
851 pr_debug("%s: wrote %i chars\n", __func__, n);
853 } else if (n == -EIO) {
854 __set_state(hp, HVSI_FSP_DIED);
855 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
859 /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
860 static void hvsi_write_worker(struct work_struct *work)
862 struct hvsi_struct *hp =
863 container_of(work, struct hvsi_struct, writer.work);
866 static long start_j = 0;
872 spin_lock_irqsave(&hp->lock, flags);
874 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
878 * We could have a non-open connection if the service processor died
879 * while we were busily scheduling ourselves. In that case, it could
880 * be minutes before the service processor comes back, so only try
881 * again once a second.
883 schedule_delayed_work(&hp->writer, HZ);
888 if (hp->n_outbuf > 0)
889 schedule_delayed_work(&hp->writer, 10);
892 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
896 wake_up_all(&hp->emptyq);
897 tty_port_tty_wakeup(&hp->port);
901 spin_unlock_irqrestore(&hp->lock, flags);
904 static int hvsi_write_room(struct tty_struct *tty)
906 struct hvsi_struct *hp = tty->driver_data;
908 return N_OUTBUF - hp->n_outbuf;
911 static int hvsi_chars_in_buffer(struct tty_struct *tty)
913 struct hvsi_struct *hp = tty->driver_data;
918 static int hvsi_write(struct tty_struct *tty,
919 const unsigned char *buf, int count)
921 struct hvsi_struct *hp = tty->driver_data;
922 const char *source = buf;
925 int origcount = count;
927 spin_lock_irqsave(&hp->lock, flags);
929 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
932 /* we're either closing or not yet open; don't accept data */
933 pr_debug("%s: not open\n", __func__);
938 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
939 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
940 * will see there is no room in outbuf and return.
942 while ((count > 0) && (hvsi_write_room(tty) > 0)) {
943 int chunksize = min(count, hvsi_write_room(tty));
945 BUG_ON(hp->n_outbuf < 0);
946 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
947 hp->n_outbuf += chunksize;
955 if (hp->n_outbuf > 0) {
957 * we weren't able to write it all to the hypervisor.
958 * schedule another push attempt.
960 schedule_delayed_work(&hp->writer, 10);
964 spin_unlock_irqrestore(&hp->lock, flags);
966 if (total != origcount)
967 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
974 * I have never seen throttle or unthrottle called, so this little throttle
975 * buffering scheme may or may not work.
977 static void hvsi_throttle(struct tty_struct *tty)
979 struct hvsi_struct *hp = tty->driver_data;
981 pr_debug("%s\n", __func__);
983 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
986 static void hvsi_unthrottle(struct tty_struct *tty)
988 struct hvsi_struct *hp = tty->driver_data;
991 pr_debug("%s\n", __func__);
993 spin_lock_irqsave(&hp->lock, flags);
994 if (hp->n_throttle) {
995 hvsi_send_overflow(hp);
996 tty_flip_buffer_push(&hp->port);
998 spin_unlock_irqrestore(&hp->lock, flags);
1001 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1004 static int hvsi_tiocmget(struct tty_struct *tty)
1006 struct hvsi_struct *hp = tty->driver_data;
1012 static int hvsi_tiocmset(struct tty_struct *tty,
1013 unsigned int set, unsigned int clear)
1015 struct hvsi_struct *hp = tty->driver_data;
1016 unsigned long flags;
1019 /* we can only alter DTR */
1023 spin_lock_irqsave(&hp->lock, flags);
1025 new_mctrl = (hp->mctrl & ~clear) | set;
1027 if (hp->mctrl != new_mctrl) {
1028 hvsi_set_mctrl(hp, new_mctrl);
1029 hp->mctrl = new_mctrl;
1031 spin_unlock_irqrestore(&hp->lock, flags);
1037 static const struct tty_operations hvsi_ops = {
1039 .close = hvsi_close,
1040 .write = hvsi_write,
1041 .hangup = hvsi_hangup,
1042 .write_room = hvsi_write_room,
1043 .chars_in_buffer = hvsi_chars_in_buffer,
1044 .throttle = hvsi_throttle,
1045 .unthrottle = hvsi_unthrottle,
1046 .tiocmget = hvsi_tiocmget,
1047 .tiocmset = hvsi_tiocmset,
1050 static int __init hvsi_init(void)
1054 hvsi_driver = alloc_tty_driver(hvsi_count);
1058 hvsi_driver->driver_name = "hvsi";
1059 hvsi_driver->name = "hvsi";
1060 hvsi_driver->major = HVSI_MAJOR;
1061 hvsi_driver->minor_start = HVSI_MINOR;
1062 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1063 hvsi_driver->init_termios = tty_std_termios;
1064 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1065 hvsi_driver->init_termios.c_ispeed = 9600;
1066 hvsi_driver->init_termios.c_ospeed = 9600;
1067 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1068 tty_set_operations(hvsi_driver, &hvsi_ops);
1070 for (i=0; i < hvsi_count; i++) {
1071 struct hvsi_struct *hp = &hvsi_ports[i];
1074 tty_port_link_device(&hp->port, hvsi_driver, i);
1076 ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp);
1078 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1081 hvsi_wait = wait_for_state; /* irqs active now */
1083 if (tty_register_driver(hvsi_driver))
1084 panic("Couldn't register hvsi console driver\n");
1086 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1090 device_initcall(hvsi_init);
1092 /***** console (not tty) code: *****/
1094 static void hvsi_console_print(struct console *console, const char *buf,
1097 struct hvsi_struct *hp = &hvsi_ports[console->index];
1098 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1099 unsigned int i = 0, n = 0;
1100 int ret, donecr = 0;
1107 * ugh, we have to translate LF -> CRLF ourselves, in place.
1108 * copied from hvc_console.c:
1110 while (count > 0 || i > 0) {
1111 if (count > 0 && i < sizeof(c)) {
1112 if (buf[n] == '\n' && !donecr) {
1121 ret = hvsi_put_chars(hp, c, i);
1129 static struct tty_driver *hvsi_console_device(struct console *console,
1132 *index = console->index;
1136 static int __init hvsi_console_setup(struct console *console, char *options)
1138 struct hvsi_struct *hp;
1141 if (console->index < 0 || console->index >= hvsi_count)
1143 hp = &hvsi_ports[console->index];
1145 /* give the FSP a chance to change the baud rate when we re-open */
1146 hvsi_close_protocol(hp);
1148 ret = hvsi_handshake(hp);
1152 ret = hvsi_get_mctrl(hp);
1156 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1160 hp->flags |= HVSI_CONSOLE;
1165 static struct console hvsi_console = {
1167 .write = hvsi_console_print,
1168 .device = hvsi_console_device,
1169 .setup = hvsi_console_setup,
1170 .flags = CON_PRINTBUFFER,
1174 static int __init hvsi_console_init(void)
1176 struct device_node *vty;
1178 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1180 /* search device tree for vty nodes */
1181 for_each_compatible_node(vty, "serial", "hvterm-protocol") {
1182 struct hvsi_struct *hp;
1183 const uint32_t *vtermno, *irq;
1185 vtermno = of_get_property(vty, "reg", NULL);
1186 irq = of_get_property(vty, "interrupts", NULL);
1187 if (!vtermno || !irq)
1190 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1195 hp = &hvsi_ports[hvsi_count];
1196 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1197 INIT_WORK(&hp->handshaker, hvsi_handshaker);
1198 init_waitqueue_head(&hp->emptyq);
1199 init_waitqueue_head(&hp->stateq);
1200 spin_lock_init(&hp->lock);
1201 tty_port_init(&hp->port);
1202 hp->index = hvsi_count;
1203 hp->inbuf_end = hp->inbuf;
1204 hp->state = HVSI_CLOSED;
1205 hp->vtermno = *vtermno;
1206 hp->virq = irq_create_mapping(NULL, irq[0]);
1207 if (hp->virq == 0) {
1208 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
1210 tty_port_destroy(&hp->port);
1218 register_console(&hvsi_console);
1221 console_initcall(hvsi_console_init);