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/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))))
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 static inline int is_console(struct hvsi_struct
*hp
)
115 return hp
->flags
& HVSI_CONSOLE
;
118 static inline int is_open(struct hvsi_struct
*hp
)
120 /* if we're waiting for an mctrl then we're already open */
121 return (hp
->state
== HVSI_OPEN
)
122 || (hp
->state
== HVSI_WAIT_FOR_MCTRL_RESPONSE
);
125 static inline void print_state(struct hvsi_struct
*hp
)
128 static const char *state_names
[] = {
130 "HVSI_WAIT_FOR_VER_RESPONSE",
131 "HVSI_WAIT_FOR_VER_QUERY",
133 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
136 const char *name
= (hp
->state
< ARRAY_SIZE(state_names
))
137 ? state_names
[hp
->state
] : "UNKNOWN";
139 pr_debug("hvsi%i: state = %s\n", hp
->index
, name
);
143 static inline void __set_state(struct hvsi_struct
*hp
, int state
)
147 wake_up_all(&hp
->stateq
);
150 static inline void set_state(struct hvsi_struct
*hp
, int state
)
154 spin_lock_irqsave(&hp
->lock
, flags
);
155 __set_state(hp
, state
);
156 spin_unlock_irqrestore(&hp
->lock
, flags
);
159 static inline int len_packet(const uint8_t *packet
)
161 return (int)((struct hvsi_header
*)packet
)->len
;
164 static inline int is_header(const uint8_t *packet
)
166 struct hvsi_header
*header
= (struct hvsi_header
*)packet
;
167 return header
->type
>= VS_QUERY_RESPONSE_PACKET_HEADER
;
170 static inline int got_packet(const struct hvsi_struct
*hp
, uint8_t *packet
)
172 if (hp
->inbuf_end
< packet
+ sizeof(struct hvsi_header
))
173 return 0; /* don't even have the packet header */
175 if (hp
->inbuf_end
< (packet
+ len_packet(packet
)))
176 return 0; /* don't have the rest of the packet */
181 /* shift remaining bytes in packetbuf down */
182 static void compact_inbuf(struct hvsi_struct
*hp
, uint8_t *read_to
)
184 int remaining
= (int)(hp
->inbuf_end
- read_to
);
186 pr_debug("%s: %i chars remain\n", __func__
, remaining
);
188 if (read_to
!= hp
->inbuf
)
189 memmove(hp
->inbuf
, read_to
, remaining
);
191 hp
->inbuf_end
= hp
->inbuf
+ remaining
;
195 #define dbg_dump_packet(packet) dump_packet(packet)
196 #define dbg_dump_hex(data, len) dump_hex(data, len)
198 #define dbg_dump_packet(packet) do { } while (0)
199 #define dbg_dump_hex(data, len) do { } while (0)
202 static void dump_hex(const uint8_t *data
, int len
)
207 for (i
=0; i
< len
; i
++)
208 printk("%.2x", data
[i
]);
211 for (i
=0; i
< len
; i
++) {
212 if (isprint(data
[i
]))
213 printk("%c", data
[i
]);
220 static void dump_packet(uint8_t *packet
)
222 struct hvsi_header
*header
= (struct hvsi_header
*)packet
;
224 printk("type 0x%x, len %i, seqno %i:\n", header
->type
, header
->len
,
227 dump_hex(packet
, header
->len
);
230 static int hvsi_read(struct hvsi_struct
*hp
, char *buf
, int count
)
234 got
= hvc_get_chars(hp
->vtermno
, buf
, count
);
239 static void hvsi_recv_control(struct hvsi_struct
*hp
, uint8_t *packet
,
240 struct tty_struct
**to_hangup
, struct hvsi_struct
**to_handshake
)
242 struct hvsi_control
*header
= (struct hvsi_control
*)packet
;
244 switch (header
->verb
) {
245 case VSV_MODEM_CTL_UPDATE
:
246 if ((header
->word
& HVSI_TSCD
) == 0) {
247 /* CD went away; no more connection */
248 pr_debug("hvsi%i: CD dropped\n", hp
->index
);
249 hp
->mctrl
&= TIOCM_CD
;
250 /* If userland hasn't done an open(2) yet, hp->tty is NULL. */
251 if (hp
->tty
&& !(hp
->tty
->flags
& CLOCAL
))
252 *to_hangup
= hp
->tty
;
255 case VSV_CLOSE_PROTOCOL
:
256 pr_debug("hvsi%i: service processor came back\n", hp
->index
);
257 if (hp
->state
!= HVSI_CLOSED
) {
262 printk(KERN_WARNING
"hvsi%i: unknown HVSI control packet: ",
269 static void hvsi_recv_response(struct hvsi_struct
*hp
, uint8_t *packet
)
271 struct hvsi_query_response
*resp
= (struct hvsi_query_response
*)packet
;
274 case HVSI_WAIT_FOR_VER_RESPONSE
:
275 __set_state(hp
, HVSI_WAIT_FOR_VER_QUERY
);
277 case HVSI_WAIT_FOR_MCTRL_RESPONSE
:
279 if (resp
->u
.mctrl_word
& HVSI_TSDTR
)
280 hp
->mctrl
|= TIOCM_DTR
;
281 if (resp
->u
.mctrl_word
& HVSI_TSCD
)
282 hp
->mctrl
|= TIOCM_CD
;
283 __set_state(hp
, HVSI_OPEN
);
286 printk(KERN_ERR
"hvsi%i: unexpected query response: ", hp
->index
);
292 /* respond to service processor's version query */
293 static int hvsi_version_respond(struct hvsi_struct
*hp
, uint16_t query_seqno
)
295 struct hvsi_query_response packet __ALIGNED__
;
298 packet
.hdr
.type
= VS_QUERY_RESPONSE_PACKET_HEADER
;
299 packet
.hdr
.len
= sizeof(struct hvsi_query_response
);
300 packet
.hdr
.seqno
= atomic_inc_return(&hp
->seqno
);
301 packet
.verb
= VSV_SEND_VERSION_NUMBER
;
302 packet
.u
.version
= HVSI_VERSION
;
303 packet
.query_seqno
= query_seqno
+1;
305 pr_debug("%s: sending %i bytes\n", __func__
, packet
.hdr
.len
);
306 dbg_dump_hex((uint8_t*)&packet
, packet
.hdr
.len
);
308 wrote
= hvc_put_chars(hp
->vtermno
, (char *)&packet
, packet
.hdr
.len
);
309 if (wrote
!= packet
.hdr
.len
) {
310 printk(KERN_ERR
"hvsi%i: couldn't send query response!\n",
318 static void hvsi_recv_query(struct hvsi_struct
*hp
, uint8_t *packet
)
320 struct hvsi_query
*query
= (struct hvsi_query
*)packet
;
323 case HVSI_WAIT_FOR_VER_QUERY
:
324 hvsi_version_respond(hp
, query
->hdr
.seqno
);
325 __set_state(hp
, HVSI_OPEN
);
328 printk(KERN_ERR
"hvsi%i: unexpected query: ", hp
->index
);
334 static void hvsi_insert_chars(struct hvsi_struct
*hp
, const char *buf
, int len
)
338 for (i
=0; i
< len
; i
++) {
340 #ifdef CONFIG_MAGIC_SYSRQ
344 } else if (hp
->sysrq
) {
349 #endif /* CONFIG_MAGIC_SYSRQ */
350 tty_insert_flip_char(hp
->tty
, c
, 0);
355 * We could get 252 bytes of data at once here. But the tty layer only
356 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
357 * it. Accordingly we won't send more than 128 bytes at a time to the flip
358 * buffer, which will give the tty buffer a chance to throttle us. Should the
359 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
362 #define TTY_THRESHOLD_THROTTLE 128
363 static struct tty_struct
*hvsi_recv_data(struct hvsi_struct
*hp
,
364 const uint8_t *packet
)
366 const struct hvsi_header
*header
= (const struct hvsi_header
*)packet
;
367 const uint8_t *data
= packet
+ sizeof(struct hvsi_header
);
368 int datalen
= header
->len
- sizeof(struct hvsi_header
);
369 int overflow
= datalen
- TTY_THRESHOLD_THROTTLE
;
371 pr_debug("queueing %i chars '%.*s'\n", datalen
, datalen
, data
);
377 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__
);
378 datalen
= TTY_THRESHOLD_THROTTLE
;
381 hvsi_insert_chars(hp
, data
, datalen
);
385 * we still have more data to deliver, so we need to save off the
386 * overflow and send it later
388 pr_debug("%s: deferring overflow\n", __func__
);
389 memcpy(hp
->throttle_buf
, data
+ TTY_THRESHOLD_THROTTLE
, overflow
);
390 hp
->n_throttle
= overflow
;
397 * Returns true/false indicating data successfully read from hypervisor.
398 * Used both to get packets for tty connections and to advance the state
399 * machine during console handshaking (in which case tty = NULL and we ignore
402 static int hvsi_load_chunk(struct hvsi_struct
*hp
, struct tty_struct
**flip
,
403 struct tty_struct
**hangup
, struct hvsi_struct
**handshake
)
405 uint8_t *packet
= hp
->inbuf
;
412 chunklen
= hvsi_read(hp
, hp
->inbuf_end
, HVSI_MAX_READ
);
414 pr_debug("%s: 0-length read\n", __func__
);
418 pr_debug("%s: got %i bytes\n", __func__
, chunklen
);
419 dbg_dump_hex(hp
->inbuf_end
, chunklen
);
421 hp
->inbuf_end
+= chunklen
;
423 /* handle all completed packets */
424 while ((packet
< hp
->inbuf_end
) && got_packet(hp
, packet
)) {
425 struct hvsi_header
*header
= (struct hvsi_header
*)packet
;
427 if (!is_header(packet
)) {
428 printk(KERN_ERR
"hvsi%i: got malformed packet\n", hp
->index
);
429 /* skip bytes until we find a header or run out of data */
430 while ((packet
< hp
->inbuf_end
) && (!is_header(packet
)))
435 pr_debug("%s: handling %i-byte packet\n", __func__
,
437 dbg_dump_packet(packet
);
439 switch (header
->type
) {
440 case VS_DATA_PACKET_HEADER
:
444 break; /* no tty buffer to put data in */
445 *flip
= hvsi_recv_data(hp
, packet
);
447 case VS_CONTROL_PACKET_HEADER
:
448 hvsi_recv_control(hp
, packet
, hangup
, handshake
);
450 case VS_QUERY_RESPONSE_PACKET_HEADER
:
451 hvsi_recv_response(hp
, packet
);
453 case VS_QUERY_PACKET_HEADER
:
454 hvsi_recv_query(hp
, packet
);
457 printk(KERN_ERR
"hvsi%i: unknown HVSI packet type 0x%x\n",
458 hp
->index
, header
->type
);
463 packet
+= len_packet(packet
);
465 if (*hangup
|| *handshake
) {
466 pr_debug("%s: hangup or handshake\n", __func__
);
468 * we need to send the hangup now before receiving any more data.
469 * If we get "data, hangup, data", we can't deliver the second
470 * data before the hangup.
476 compact_inbuf(hp
, packet
);
481 static void hvsi_send_overflow(struct hvsi_struct
*hp
)
483 pr_debug("%s: delivering %i bytes overflow\n", __func__
,
486 hvsi_insert_chars(hp
, hp
->throttle_buf
, hp
->n_throttle
);
491 * must get all pending data because we only get an irq on empty->non-empty
494 static irqreturn_t
hvsi_interrupt(int irq
, void *arg
)
496 struct hvsi_struct
*hp
= (struct hvsi_struct
*)arg
;
497 struct tty_struct
*flip
;
498 struct tty_struct
*hangup
;
499 struct hvsi_struct
*handshake
;
503 pr_debug("%s\n", __func__
);
506 spin_lock_irqsave(&hp
->lock
, flags
);
507 again
= hvsi_load_chunk(hp
, &flip
, &hangup
, &handshake
);
508 spin_unlock_irqrestore(&hp
->lock
, flags
);
511 * we have to call tty_flip_buffer_push() and tty_hangup() outside our
512 * spinlock. But we also have to keep going until we've read all the
517 /* there was data put in the tty flip buffer */
518 tty_flip_buffer_push(flip
);
527 pr_debug("hvsi%i: attempting re-handshake\n", handshake
->index
);
528 schedule_work(&handshake
->handshaker
);
532 spin_lock_irqsave(&hp
->lock
, flags
);
533 if (hp
->tty
&& hp
->n_throttle
534 && (!test_bit(TTY_THROTTLED
, &hp
->tty
->flags
))) {
535 /* we weren't hung up and we weren't throttled, so we can deliver the
538 hvsi_send_overflow(hp
);
540 spin_unlock_irqrestore(&hp
->lock
, flags
);
543 tty_flip_buffer_push(flip
);
549 /* for boot console, before the irq handler is running */
550 static int __init
poll_for_state(struct hvsi_struct
*hp
, int state
)
552 unsigned long end_jiffies
= jiffies
+ HVSI_TIMEOUT
;
555 hvsi_interrupt(hp
->virq
, (void *)hp
); /* get pending data */
557 if (hp
->state
== state
)
561 if (time_after(jiffies
, end_jiffies
))
566 /* wait for irq handler to change our state */
567 static int wait_for_state(struct hvsi_struct
*hp
, int state
)
571 if (!wait_event_timeout(hp
->stateq
, (hp
->state
== state
), HVSI_TIMEOUT
))
577 static int hvsi_query(struct hvsi_struct
*hp
, uint16_t verb
)
579 struct hvsi_query packet __ALIGNED__
;
582 packet
.hdr
.type
= VS_QUERY_PACKET_HEADER
;
583 packet
.hdr
.len
= sizeof(struct hvsi_query
);
584 packet
.hdr
.seqno
= atomic_inc_return(&hp
->seqno
);
587 pr_debug("%s: sending %i bytes\n", __func__
, packet
.hdr
.len
);
588 dbg_dump_hex((uint8_t*)&packet
, packet
.hdr
.len
);
590 wrote
= hvc_put_chars(hp
->vtermno
, (char *)&packet
, packet
.hdr
.len
);
591 if (wrote
!= packet
.hdr
.len
) {
592 printk(KERN_ERR
"hvsi%i: couldn't send query (%i)!\n", hp
->index
,
600 static int hvsi_get_mctrl(struct hvsi_struct
*hp
)
604 set_state(hp
, HVSI_WAIT_FOR_MCTRL_RESPONSE
);
605 hvsi_query(hp
, VSV_SEND_MODEM_CTL_STATUS
);
607 ret
= hvsi_wait(hp
, HVSI_OPEN
);
609 printk(KERN_ERR
"hvsi%i: didn't get modem flags\n", hp
->index
);
610 set_state(hp
, HVSI_OPEN
);
614 pr_debug("%s: mctrl 0x%x\n", __func__
, hp
->mctrl
);
619 /* note that we can only set DTR */
620 static int hvsi_set_mctrl(struct hvsi_struct
*hp
, uint16_t mctrl
)
622 struct hvsi_control packet __ALIGNED__
;
625 packet
.hdr
.type
= VS_CONTROL_PACKET_HEADER
,
626 packet
.hdr
.seqno
= atomic_inc_return(&hp
->seqno
);
627 packet
.hdr
.len
= sizeof(struct hvsi_control
);
628 packet
.verb
= VSV_SET_MODEM_CTL
;
629 packet
.mask
= HVSI_TSDTR
;
631 if (mctrl
& TIOCM_DTR
)
632 packet
.word
= HVSI_TSDTR
;
634 pr_debug("%s: sending %i bytes\n", __func__
, packet
.hdr
.len
);
635 dbg_dump_hex((uint8_t*)&packet
, packet
.hdr
.len
);
637 wrote
= hvc_put_chars(hp
->vtermno
, (char *)&packet
, packet
.hdr
.len
);
638 if (wrote
!= packet
.hdr
.len
) {
639 printk(KERN_ERR
"hvsi%i: couldn't set DTR!\n", hp
->index
);
646 static void hvsi_drain_input(struct hvsi_struct
*hp
)
648 uint8_t buf
[HVSI_MAX_READ
] __ALIGNED__
;
649 unsigned long end_jiffies
= jiffies
+ HVSI_TIMEOUT
;
651 while (time_before(end_jiffies
, jiffies
))
652 if (0 == hvsi_read(hp
, buf
, HVSI_MAX_READ
))
656 static int hvsi_handshake(struct hvsi_struct
*hp
)
661 * We could have a CLOSE or other data waiting for us before we even try
662 * to open; try to throw it all away so we don't get confused. (CLOSE
663 * is the first message sent up the pipe when the FSP comes online. We
664 * need to distinguish between "it came up a while ago and we're the first
665 * user" and "it was just reset before it saw our handshake packet".)
667 hvsi_drain_input(hp
);
669 set_state(hp
, HVSI_WAIT_FOR_VER_RESPONSE
);
670 ret
= hvsi_query(hp
, VSV_SEND_VERSION_NUMBER
);
672 printk(KERN_ERR
"hvsi%i: couldn't send version query\n", hp
->index
);
676 ret
= hvsi_wait(hp
, HVSI_OPEN
);
683 static void hvsi_handshaker(struct work_struct
*work
)
685 struct hvsi_struct
*hp
=
686 container_of(work
, struct hvsi_struct
, handshaker
);
688 if (hvsi_handshake(hp
) >= 0)
691 printk(KERN_ERR
"hvsi%i: re-handshaking failed\n", hp
->index
);
692 if (is_console(hp
)) {
694 * ttys will re-attempt the handshake via hvsi_open, but
695 * the console will not.
697 printk(KERN_ERR
"hvsi%i: lost console!\n", hp
->index
);
701 static int hvsi_put_chars(struct hvsi_struct
*hp
, const char *buf
, int count
)
703 struct hvsi_data packet __ALIGNED__
;
706 BUG_ON(count
> HVSI_MAX_OUTGOING_DATA
);
708 packet
.hdr
.type
= VS_DATA_PACKET_HEADER
;
709 packet
.hdr
.seqno
= atomic_inc_return(&hp
->seqno
);
710 packet
.hdr
.len
= count
+ sizeof(struct hvsi_header
);
711 memcpy(&packet
.data
, buf
, count
);
713 ret
= hvc_put_chars(hp
->vtermno
, (char *)&packet
, packet
.hdr
.len
);
714 if (ret
== packet
.hdr
.len
) {
715 /* return the number of chars written, not the packet length */
718 return ret
; /* return any errors */
721 static void hvsi_close_protocol(struct hvsi_struct
*hp
)
723 struct hvsi_control packet __ALIGNED__
;
725 packet
.hdr
.type
= VS_CONTROL_PACKET_HEADER
;
726 packet
.hdr
.seqno
= atomic_inc_return(&hp
->seqno
);
728 packet
.verb
= VSV_CLOSE_PROTOCOL
;
730 pr_debug("%s: sending %i bytes\n", __func__
, packet
.hdr
.len
);
731 dbg_dump_hex((uint8_t*)&packet
, packet
.hdr
.len
);
733 hvc_put_chars(hp
->vtermno
, (char *)&packet
, packet
.hdr
.len
);
736 static int hvsi_open(struct tty_struct
*tty
, struct file
*filp
)
738 struct hvsi_struct
*hp
;
740 int line
= tty
->index
;
743 pr_debug("%s\n", __func__
);
745 if (line
< 0 || line
>= hvsi_count
)
747 hp
= &hvsi_ports
[line
];
749 tty
->driver_data
= hp
;
752 if (hp
->state
== HVSI_FSP_DIED
)
755 spin_lock_irqsave(&hp
->lock
, flags
);
758 atomic_set(&hp
->seqno
, 0);
759 h_vio_signal(hp
->vtermno
, VIO_IRQ_ENABLE
);
760 spin_unlock_irqrestore(&hp
->lock
, flags
);
763 return 0; /* this has already been handshaked as the console */
765 ret
= hvsi_handshake(hp
);
767 printk(KERN_ERR
"%s: HVSI handshaking failed\n", tty
->name
);
771 ret
= hvsi_get_mctrl(hp
);
773 printk(KERN_ERR
"%s: couldn't get initial modem flags\n", tty
->name
);
777 ret
= hvsi_set_mctrl(hp
, hp
->mctrl
| TIOCM_DTR
);
779 printk(KERN_ERR
"%s: couldn't set DTR\n", tty
->name
);
786 /* wait for hvsi_write_worker to empty hp->outbuf */
787 static void hvsi_flush_output(struct hvsi_struct
*hp
)
789 wait_event_timeout(hp
->emptyq
, (hp
->n_outbuf
<= 0), HVSI_TIMEOUT
);
791 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
792 cancel_delayed_work_sync(&hp
->writer
);
793 flush_work_sync(&hp
->handshaker
);
796 * it's also possible that our timeout expired and hvsi_write_worker
797 * didn't manage to push outbuf. poof.
802 static void hvsi_close(struct tty_struct
*tty
, struct file
*filp
)
804 struct hvsi_struct
*hp
= tty
->driver_data
;
807 pr_debug("%s\n", __func__
);
809 if (tty_hung_up_p(filp
))
812 spin_lock_irqsave(&hp
->lock
, flags
);
814 if (--hp
->count
== 0) {
816 hp
->inbuf_end
= hp
->inbuf
; /* discard remaining partial packets */
818 /* only close down connection if it is not the console */
819 if (!is_console(hp
)) {
820 h_vio_signal(hp
->vtermno
, VIO_IRQ_DISABLE
); /* no more irqs */
821 __set_state(hp
, HVSI_CLOSED
);
823 * any data delivered to the tty layer after this will be
824 * discarded (except for XON/XOFF)
828 spin_unlock_irqrestore(&hp
->lock
, flags
);
830 /* let any existing irq handlers finish. no more will start. */
831 synchronize_irq(hp
->virq
);
833 /* hvsi_write_worker will re-schedule until outbuf is empty. */
834 hvsi_flush_output(hp
);
836 /* tell FSP to stop sending data */
837 hvsi_close_protocol(hp
);
840 * drain anything FSP is still in the middle of sending, and let
841 * hvsi_handshake drain the rest on the next open.
843 hvsi_drain_input(hp
);
845 spin_lock_irqsave(&hp
->lock
, flags
);
847 } else if (hp
->count
< 0)
848 printk(KERN_ERR
"hvsi_close %lu: oops, count is %d\n",
849 hp
- hvsi_ports
, hp
->count
);
851 spin_unlock_irqrestore(&hp
->lock
, flags
);
854 static void hvsi_hangup(struct tty_struct
*tty
)
856 struct hvsi_struct
*hp
= tty
->driver_data
;
859 pr_debug("%s\n", __func__
);
861 spin_lock_irqsave(&hp
->lock
, flags
);
867 spin_unlock_irqrestore(&hp
->lock
, flags
);
870 /* called with hp->lock held */
871 static void hvsi_push(struct hvsi_struct
*hp
)
875 if (hp
->n_outbuf
<= 0)
878 n
= hvsi_put_chars(hp
, hp
->outbuf
, hp
->n_outbuf
);
881 pr_debug("%s: wrote %i chars\n", __func__
, n
);
883 } else if (n
== -EIO
) {
884 __set_state(hp
, HVSI_FSP_DIED
);
885 printk(KERN_ERR
"hvsi%i: service processor died\n", hp
->index
);
889 /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
890 static void hvsi_write_worker(struct work_struct
*work
)
892 struct hvsi_struct
*hp
=
893 container_of(work
, struct hvsi_struct
, writer
.work
);
896 static long start_j
= 0;
902 spin_lock_irqsave(&hp
->lock
, flags
);
904 pr_debug("%s: %i chars in buffer\n", __func__
, hp
->n_outbuf
);
908 * We could have a non-open connection if the service processor died
909 * while we were busily scheduling ourselves. In that case, it could
910 * be minutes before the service processor comes back, so only try
911 * again once a second.
913 schedule_delayed_work(&hp
->writer
, HZ
);
918 if (hp
->n_outbuf
> 0)
919 schedule_delayed_work(&hp
->writer
, 10);
922 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__
,
926 wake_up_all(&hp
->emptyq
);
931 spin_unlock_irqrestore(&hp
->lock
, flags
);
934 static int hvsi_write_room(struct tty_struct
*tty
)
936 struct hvsi_struct
*hp
= tty
->driver_data
;
938 return N_OUTBUF
- hp
->n_outbuf
;
941 static int hvsi_chars_in_buffer(struct tty_struct
*tty
)
943 struct hvsi_struct
*hp
= tty
->driver_data
;
948 static int hvsi_write(struct tty_struct
*tty
,
949 const unsigned char *buf
, int count
)
951 struct hvsi_struct
*hp
= tty
->driver_data
;
952 const char *source
= buf
;
955 int origcount
= count
;
957 spin_lock_irqsave(&hp
->lock
, flags
);
959 pr_debug("%s: %i chars in buffer\n", __func__
, hp
->n_outbuf
);
962 /* we're either closing or not yet open; don't accept data */
963 pr_debug("%s: not open\n", __func__
);
968 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
969 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
970 * will see there is no room in outbuf and return.
972 while ((count
> 0) && (hvsi_write_room(hp
->tty
) > 0)) {
973 int chunksize
= min(count
, hvsi_write_room(hp
->tty
));
975 BUG_ON(hp
->n_outbuf
< 0);
976 memcpy(hp
->outbuf
+ hp
->n_outbuf
, source
, chunksize
);
977 hp
->n_outbuf
+= chunksize
;
985 if (hp
->n_outbuf
> 0) {
987 * we weren't able to write it all to the hypervisor.
988 * schedule another push attempt.
990 schedule_delayed_work(&hp
->writer
, 10);
994 spin_unlock_irqrestore(&hp
->lock
, flags
);
996 if (total
!= origcount
)
997 pr_debug("%s: wanted %i, only wrote %i\n", __func__
, origcount
,
1004 * I have never seen throttle or unthrottle called, so this little throttle
1005 * buffering scheme may or may not work.
1007 static void hvsi_throttle(struct tty_struct
*tty
)
1009 struct hvsi_struct
*hp
= tty
->driver_data
;
1011 pr_debug("%s\n", __func__
);
1013 h_vio_signal(hp
->vtermno
, VIO_IRQ_DISABLE
);
1016 static void hvsi_unthrottle(struct tty_struct
*tty
)
1018 struct hvsi_struct
*hp
= tty
->driver_data
;
1019 unsigned long flags
;
1022 pr_debug("%s\n", __func__
);
1024 spin_lock_irqsave(&hp
->lock
, flags
);
1025 if (hp
->n_throttle
) {
1026 hvsi_send_overflow(hp
);
1029 spin_unlock_irqrestore(&hp
->lock
, flags
);
1032 tty_flip_buffer_push(hp
->tty
);
1034 h_vio_signal(hp
->vtermno
, VIO_IRQ_ENABLE
);
1037 static int hvsi_tiocmget(struct tty_struct
*tty
)
1039 struct hvsi_struct
*hp
= tty
->driver_data
;
1045 static int hvsi_tiocmset(struct tty_struct
*tty
,
1046 unsigned int set
, unsigned int clear
)
1048 struct hvsi_struct
*hp
= tty
->driver_data
;
1049 unsigned long flags
;
1052 /* we can only alter DTR */
1056 spin_lock_irqsave(&hp
->lock
, flags
);
1058 new_mctrl
= (hp
->mctrl
& ~clear
) | set
;
1060 if (hp
->mctrl
!= new_mctrl
) {
1061 hvsi_set_mctrl(hp
, new_mctrl
);
1062 hp
->mctrl
= new_mctrl
;
1064 spin_unlock_irqrestore(&hp
->lock
, flags
);
1070 static const struct tty_operations hvsi_ops
= {
1072 .close
= hvsi_close
,
1073 .write
= hvsi_write
,
1074 .hangup
= hvsi_hangup
,
1075 .write_room
= hvsi_write_room
,
1076 .chars_in_buffer
= hvsi_chars_in_buffer
,
1077 .throttle
= hvsi_throttle
,
1078 .unthrottle
= hvsi_unthrottle
,
1079 .tiocmget
= hvsi_tiocmget
,
1080 .tiocmset
= hvsi_tiocmset
,
1083 static int __init
hvsi_init(void)
1087 hvsi_driver
= alloc_tty_driver(hvsi_count
);
1091 hvsi_driver
->owner
= THIS_MODULE
;
1092 hvsi_driver
->driver_name
= "hvsi";
1093 hvsi_driver
->name
= "hvsi";
1094 hvsi_driver
->major
= HVSI_MAJOR
;
1095 hvsi_driver
->minor_start
= HVSI_MINOR
;
1096 hvsi_driver
->type
= TTY_DRIVER_TYPE_SYSTEM
;
1097 hvsi_driver
->init_termios
= tty_std_termios
;
1098 hvsi_driver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
;
1099 hvsi_driver
->init_termios
.c_ispeed
= 9600;
1100 hvsi_driver
->init_termios
.c_ospeed
= 9600;
1101 hvsi_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1102 tty_set_operations(hvsi_driver
, &hvsi_ops
);
1104 for (i
=0; i
< hvsi_count
; i
++) {
1105 struct hvsi_struct
*hp
= &hvsi_ports
[i
];
1108 ret
= request_irq(hp
->virq
, hvsi_interrupt
, 0, "hvsi", hp
);
1110 printk(KERN_ERR
"HVSI: couldn't reserve irq 0x%x (error %i)\n",
1113 hvsi_wait
= wait_for_state
; /* irqs active now */
1115 if (tty_register_driver(hvsi_driver
))
1116 panic("Couldn't register hvsi console driver\n");
1118 printk(KERN_DEBUG
"HVSI: registered %i devices\n", hvsi_count
);
1122 device_initcall(hvsi_init
);
1124 /***** console (not tty) code: *****/
1126 static void hvsi_console_print(struct console
*console
, const char *buf
,
1129 struct hvsi_struct
*hp
= &hvsi_ports
[console
->index
];
1130 char c
[HVSI_MAX_OUTGOING_DATA
] __ALIGNED__
;
1131 unsigned int i
= 0, n
= 0;
1132 int ret
, donecr
= 0;
1139 * ugh, we have to translate LF -> CRLF ourselves, in place.
1140 * copied from hvc_console.c:
1142 while (count
> 0 || i
> 0) {
1143 if (count
> 0 && i
< sizeof(c
)) {
1144 if (buf
[n
] == '\n' && !donecr
) {
1153 ret
= hvsi_put_chars(hp
, c
, i
);
1161 static struct tty_driver
*hvsi_console_device(struct console
*console
,
1164 *index
= console
->index
;
1168 static int __init
hvsi_console_setup(struct console
*console
, char *options
)
1170 struct hvsi_struct
*hp
;
1173 if (console
->index
< 0 || console
->index
>= hvsi_count
)
1175 hp
= &hvsi_ports
[console
->index
];
1177 /* give the FSP a chance to change the baud rate when we re-open */
1178 hvsi_close_protocol(hp
);
1180 ret
= hvsi_handshake(hp
);
1184 ret
= hvsi_get_mctrl(hp
);
1188 ret
= hvsi_set_mctrl(hp
, hp
->mctrl
| TIOCM_DTR
);
1192 hp
->flags
|= HVSI_CONSOLE
;
1197 static struct console hvsi_console
= {
1199 .write
= hvsi_console_print
,
1200 .device
= hvsi_console_device
,
1201 .setup
= hvsi_console_setup
,
1202 .flags
= CON_PRINTBUFFER
,
1206 static int __init
hvsi_console_init(void)
1208 struct device_node
*vty
;
1210 hvsi_wait
= poll_for_state
; /* no irqs yet; must poll */
1212 /* search device tree for vty nodes */
1213 for (vty
= of_find_compatible_node(NULL
, "serial", "hvterm-protocol");
1215 vty
= of_find_compatible_node(vty
, "serial", "hvterm-protocol")) {
1216 struct hvsi_struct
*hp
;
1217 const uint32_t *vtermno
, *irq
;
1219 vtermno
= of_get_property(vty
, "reg", NULL
);
1220 irq
= of_get_property(vty
, "interrupts", NULL
);
1221 if (!vtermno
|| !irq
)
1224 if (hvsi_count
>= MAX_NR_HVSI_CONSOLES
) {
1229 hp
= &hvsi_ports
[hvsi_count
];
1230 INIT_DELAYED_WORK(&hp
->writer
, hvsi_write_worker
);
1231 INIT_WORK(&hp
->handshaker
, hvsi_handshaker
);
1232 init_waitqueue_head(&hp
->emptyq
);
1233 init_waitqueue_head(&hp
->stateq
);
1234 spin_lock_init(&hp
->lock
);
1235 hp
->index
= hvsi_count
;
1236 hp
->inbuf_end
= hp
->inbuf
;
1237 hp
->state
= HVSI_CLOSED
;
1238 hp
->vtermno
= *vtermno
;
1239 hp
->virq
= irq_create_mapping(NULL
, irq
[0]);
1240 if (hp
->virq
== NO_IRQ
) {
1241 printk(KERN_ERR
"%s: couldn't create irq mapping for 0x%x\n",
1250 register_console(&hvsi_console
);
1253 console_initcall(hvsi_console_init
);