• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/char/
1/*
2 * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM
3 *
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.
8 *
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.
13 *
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
17 */
18
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.
25 *
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.
30 */
31
32#undef DEBUG
33
34#include <linux/console.h>
35#include <linux/ctype.h>
36#include <linux/delay.h>
37#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/module.h>
40#include <linux/major.h>
41#include <linux/kernel.h>
42#include <linux/spinlock.h>
43#include <linux/sysrq.h>
44#include <linux/tty.h>
45#include <linux/tty_flip.h>
46#include <asm/hvcall.h>
47#include <asm/hvconsole.h>
48#include <asm/prom.h>
49#include <asm/uaccess.h>
50#include <asm/vio.h>
51#include <asm/param.h>
52
53#define HVSI_MAJOR	229
54#define HVSI_MINOR	128
55#define MAX_NR_HVSI_CONSOLES 4
56
57#define HVSI_TIMEOUT (5*HZ)
58#define HVSI_VERSION 1
59#define HVSI_MAX_PACKET 256
60#define HVSI_MAX_READ 16
61#define HVSI_MAX_OUTGOING_DATA 12
62#define N_OUTBUF 12
63
64/*
65 * we pass data via two 8-byte registers, so we would like our char arrays
66 * properly aligned for those loads.
67 */
68#define __ALIGNED__	__attribute__((__aligned__(sizeof(long))))
69
70struct hvsi_struct {
71	struct delayed_work writer;
72	struct work_struct handshaker;
73	wait_queue_head_t emptyq; /* woken when outbuf is emptied */
74	wait_queue_head_t stateq; /* woken when HVSI state changes */
75	spinlock_t lock;
76	int index;
77	struct tty_struct *tty;
78	int count;
79	uint8_t throttle_buf[128];
80	uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
81	/* inbuf is for packet reassembly. leave a little room for leftovers. */
82	uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
83	uint8_t *inbuf_end;
84	int n_throttle;
85	int n_outbuf;
86	uint32_t vtermno;
87	uint32_t virq;
88	atomic_t seqno; /* HVSI packet sequence number */
89	uint16_t mctrl;
90	uint8_t state;  /* HVSI protocol state */
91	uint8_t flags;
92#ifdef CONFIG_MAGIC_SYSRQ
93	uint8_t sysrq;
94#endif /* CONFIG_MAGIC_SYSRQ */
95};
96static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
97
98static struct tty_driver *hvsi_driver;
99static int hvsi_count;
100static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
101
102enum HVSI_PROTOCOL_STATE {
103	HVSI_CLOSED,
104	HVSI_WAIT_FOR_VER_RESPONSE,
105	HVSI_WAIT_FOR_VER_QUERY,
106	HVSI_OPEN,
107	HVSI_WAIT_FOR_MCTRL_RESPONSE,
108	HVSI_FSP_DIED,
109};
110#define HVSI_CONSOLE 0x1
111
112#define VS_DATA_PACKET_HEADER           0xff
113#define VS_CONTROL_PACKET_HEADER        0xfe
114#define VS_QUERY_PACKET_HEADER          0xfd
115#define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc
116
117/* control verbs */
118#define VSV_SET_MODEM_CTL    1 /* to service processor only */
119#define VSV_MODEM_CTL_UPDATE 2 /* from service processor only */
120#define VSV_CLOSE_PROTOCOL   3
121
122/* query verbs */
123#define VSV_SEND_VERSION_NUMBER 1
124#define VSV_SEND_MODEM_CTL_STATUS 2
125
126/* yes, these masks are not consecutive. */
127#define HVSI_TSDTR 0x01
128#define HVSI_TSCD  0x20
129
130struct hvsi_header {
131	uint8_t  type;
132	uint8_t  len;
133	uint16_t seqno;
134} __attribute__((packed));
135
136struct hvsi_data {
137	uint8_t  type;
138	uint8_t  len;
139	uint16_t seqno;
140	uint8_t  data[HVSI_MAX_OUTGOING_DATA];
141} __attribute__((packed));
142
143struct hvsi_control {
144	uint8_t  type;
145	uint8_t  len;
146	uint16_t seqno;
147	uint16_t verb;
148	/* optional depending on verb: */
149	uint32_t word;
150	uint32_t mask;
151} __attribute__((packed));
152
153struct hvsi_query {
154	uint8_t  type;
155	uint8_t  len;
156	uint16_t seqno;
157	uint16_t verb;
158} __attribute__((packed));
159
160struct hvsi_query_response {
161	uint8_t  type;
162	uint8_t  len;
163	uint16_t seqno;
164	uint16_t verb;
165	uint16_t query_seqno;
166	union {
167		uint8_t  version;
168		uint32_t mctrl_word;
169	} u;
170} __attribute__((packed));
171
172
173
174static inline int is_console(struct hvsi_struct *hp)
175{
176	return hp->flags & HVSI_CONSOLE;
177}
178
179static inline int is_open(struct hvsi_struct *hp)
180{
181	/* if we're waiting for an mctrl then we're already open */
182	return (hp->state == HVSI_OPEN)
183			|| (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
184}
185
186static inline void print_state(struct hvsi_struct *hp)
187{
188#ifdef DEBUG
189	static const char *state_names[] = {
190		"HVSI_CLOSED",
191		"HVSI_WAIT_FOR_VER_RESPONSE",
192		"HVSI_WAIT_FOR_VER_QUERY",
193		"HVSI_OPEN",
194		"HVSI_WAIT_FOR_MCTRL_RESPONSE",
195		"HVSI_FSP_DIED",
196	};
197	const char *name = (hp->state < ARRAY_SIZE(state_names))
198		? state_names[hp->state] : "UNKNOWN";
199
200	pr_debug("hvsi%i: state = %s\n", hp->index, name);
201#endif /* DEBUG */
202}
203
204static inline void __set_state(struct hvsi_struct *hp, int state)
205{
206	hp->state = state;
207	print_state(hp);
208	wake_up_all(&hp->stateq);
209}
210
211static inline void set_state(struct hvsi_struct *hp, int state)
212{
213	unsigned long flags;
214
215	spin_lock_irqsave(&hp->lock, flags);
216	__set_state(hp, state);
217	spin_unlock_irqrestore(&hp->lock, flags);
218}
219
220static inline int len_packet(const uint8_t *packet)
221{
222	return (int)((struct hvsi_header *)packet)->len;
223}
224
225static inline int is_header(const uint8_t *packet)
226{
227	struct hvsi_header *header = (struct hvsi_header *)packet;
228	return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
229}
230
231static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
232{
233	if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
234		return 0; /* don't even have the packet header */
235
236	if (hp->inbuf_end < (packet + len_packet(packet)))
237		return 0; /* don't have the rest of the packet */
238
239	return 1;
240}
241
242/* shift remaining bytes in packetbuf down */
243static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
244{
245	int remaining = (int)(hp->inbuf_end - read_to);
246
247	pr_debug("%s: %i chars remain\n", __func__, remaining);
248
249	if (read_to != hp->inbuf)
250		memmove(hp->inbuf, read_to, remaining);
251
252	hp->inbuf_end = hp->inbuf + remaining;
253}
254
255#ifdef DEBUG
256#define dbg_dump_packet(packet) dump_packet(packet)
257#define dbg_dump_hex(data, len) dump_hex(data, len)
258#else
259#define dbg_dump_packet(packet) do { } while (0)
260#define dbg_dump_hex(data, len) do { } while (0)
261#endif
262
263static void dump_hex(const uint8_t *data, int len)
264{
265	int i;
266
267	printk("    ");
268	for (i=0; i < len; i++)
269		printk("%.2x", data[i]);
270
271	printk("\n    ");
272	for (i=0; i < len; i++) {
273		if (isprint(data[i]))
274			printk("%c", data[i]);
275		else
276			printk(".");
277	}
278	printk("\n");
279}
280
281static void dump_packet(uint8_t *packet)
282{
283	struct hvsi_header *header = (struct hvsi_header *)packet;
284
285	printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
286			header->seqno);
287
288	dump_hex(packet, header->len);
289}
290
291static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
292{
293	unsigned long got;
294
295	got = hvc_get_chars(hp->vtermno, buf, count);
296
297	return got;
298}
299
300static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
301	struct tty_struct **to_hangup, struct hvsi_struct **to_handshake)
302{
303	struct hvsi_control *header = (struct hvsi_control *)packet;
304
305	switch (header->verb) {
306		case VSV_MODEM_CTL_UPDATE:
307			if ((header->word & HVSI_TSCD) == 0) {
308				/* CD went away; no more connection */
309				pr_debug("hvsi%i: CD dropped\n", hp->index);
310				hp->mctrl &= TIOCM_CD;
311				/* If userland hasn't done an open(2) yet, hp->tty is NULL. */
312				if (hp->tty && !(hp->tty->flags & CLOCAL))
313					*to_hangup = hp->tty;
314			}
315			break;
316		case VSV_CLOSE_PROTOCOL:
317			pr_debug("hvsi%i: service processor came back\n", hp->index);
318			if (hp->state != HVSI_CLOSED) {
319				*to_handshake = hp;
320			}
321			break;
322		default:
323			printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
324				hp->index);
325			dump_packet(packet);
326			break;
327	}
328}
329
330static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
331{
332	struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
333
334	switch (hp->state) {
335		case HVSI_WAIT_FOR_VER_RESPONSE:
336			__set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
337			break;
338		case HVSI_WAIT_FOR_MCTRL_RESPONSE:
339			hp->mctrl = 0;
340			if (resp->u.mctrl_word & HVSI_TSDTR)
341				hp->mctrl |= TIOCM_DTR;
342			if (resp->u.mctrl_word & HVSI_TSCD)
343				hp->mctrl |= TIOCM_CD;
344			__set_state(hp, HVSI_OPEN);
345			break;
346		default:
347			printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
348			dump_packet(packet);
349			break;
350	}
351}
352
353/* respond to service processor's version query */
354static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
355{
356	struct hvsi_query_response packet __ALIGNED__;
357	int wrote;
358
359	packet.type = VS_QUERY_RESPONSE_PACKET_HEADER;
360	packet.len = sizeof(struct hvsi_query_response);
361	packet.seqno = atomic_inc_return(&hp->seqno);
362	packet.verb = VSV_SEND_VERSION_NUMBER;
363	packet.u.version = HVSI_VERSION;
364	packet.query_seqno = query_seqno+1;
365
366	pr_debug("%s: sending %i bytes\n", __func__, packet.len);
367	dbg_dump_hex((uint8_t*)&packet, packet.len);
368
369	wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
370	if (wrote != packet.len) {
371		printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
372			hp->index);
373		return -EIO;
374	}
375
376	return 0;
377}
378
379static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
380{
381	struct hvsi_query *query = (struct hvsi_query *)packet;
382
383	switch (hp->state) {
384		case HVSI_WAIT_FOR_VER_QUERY:
385			hvsi_version_respond(hp, query->seqno);
386			__set_state(hp, HVSI_OPEN);
387			break;
388		default:
389			printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
390			dump_packet(packet);
391			break;
392	}
393}
394
395static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
396{
397	int i;
398
399	for (i=0; i < len; i++) {
400		char c = buf[i];
401#ifdef CONFIG_MAGIC_SYSRQ
402		if (c == '\0') {
403			hp->sysrq = 1;
404			continue;
405		} else if (hp->sysrq) {
406			handle_sysrq(c);
407			hp->sysrq = 0;
408			continue;
409		}
410#endif /* CONFIG_MAGIC_SYSRQ */
411		tty_insert_flip_char(hp->tty, c, 0);
412	}
413}
414
415/*
416 * We could get 252 bytes of data at once here. But the tty layer only
417 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
418 * it. Accordingly we won't send more than 128 bytes at a time to the flip
419 * buffer, which will give the tty buffer a chance to throttle us. Should the
420 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
421 * revisited.
422 */
423#define TTY_THRESHOLD_THROTTLE 128
424static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
425		const uint8_t *packet)
426{
427	const struct hvsi_header *header = (const struct hvsi_header *)packet;
428	const uint8_t *data = packet + sizeof(struct hvsi_header);
429	int datalen = header->len - sizeof(struct hvsi_header);
430	int overflow = datalen - TTY_THRESHOLD_THROTTLE;
431
432	pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
433
434	if (datalen == 0)
435		return NULL;
436
437	if (overflow > 0) {
438		pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
439		datalen = TTY_THRESHOLD_THROTTLE;
440	}
441
442	hvsi_insert_chars(hp, data, datalen);
443
444	if (overflow > 0) {
445		/*
446		 * we still have more data to deliver, so we need to save off the
447		 * overflow and send it later
448		 */
449		pr_debug("%s: deferring overflow\n", __func__);
450		memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
451		hp->n_throttle = overflow;
452	}
453
454	return hp->tty;
455}
456
457/*
458 * Returns true/false indicating data successfully read from hypervisor.
459 * Used both to get packets for tty connections and to advance the state
460 * machine during console handshaking (in which case tty = NULL and we ignore
461 * incoming data).
462 */
463static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
464		struct tty_struct **hangup, struct hvsi_struct **handshake)
465{
466	uint8_t *packet = hp->inbuf;
467	int chunklen;
468
469	*flip = NULL;
470	*hangup = NULL;
471	*handshake = NULL;
472
473	chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
474	if (chunklen == 0) {
475		pr_debug("%s: 0-length read\n", __func__);
476		return 0;
477	}
478
479	pr_debug("%s: got %i bytes\n", __func__, chunklen);
480	dbg_dump_hex(hp->inbuf_end, chunklen);
481
482	hp->inbuf_end += chunklen;
483
484	/* handle all completed packets */
485	while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
486		struct hvsi_header *header = (struct hvsi_header *)packet;
487
488		if (!is_header(packet)) {
489			printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
490			/* skip bytes until we find a header or run out of data */
491			while ((packet < hp->inbuf_end) && (!is_header(packet)))
492				packet++;
493			continue;
494		}
495
496		pr_debug("%s: handling %i-byte packet\n", __func__,
497				len_packet(packet));
498		dbg_dump_packet(packet);
499
500		switch (header->type) {
501			case VS_DATA_PACKET_HEADER:
502				if (!is_open(hp))
503					break;
504				if (hp->tty == NULL)
505					break; /* no tty buffer to put data in */
506				*flip = hvsi_recv_data(hp, packet);
507				break;
508			case VS_CONTROL_PACKET_HEADER:
509				hvsi_recv_control(hp, packet, hangup, handshake);
510				break;
511			case VS_QUERY_RESPONSE_PACKET_HEADER:
512				hvsi_recv_response(hp, packet);
513				break;
514			case VS_QUERY_PACKET_HEADER:
515				hvsi_recv_query(hp, packet);
516				break;
517			default:
518				printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
519						hp->index, header->type);
520				dump_packet(packet);
521				break;
522		}
523
524		packet += len_packet(packet);
525
526		if (*hangup || *handshake) {
527			pr_debug("%s: hangup or handshake\n", __func__);
528			/*
529			 * we need to send the hangup now before receiving any more data.
530			 * If we get "data, hangup, data", we can't deliver the second
531			 * data before the hangup.
532			 */
533			break;
534		}
535	}
536
537	compact_inbuf(hp, packet);
538
539	return 1;
540}
541
542static void hvsi_send_overflow(struct hvsi_struct *hp)
543{
544	pr_debug("%s: delivering %i bytes overflow\n", __func__,
545			hp->n_throttle);
546
547	hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
548	hp->n_throttle = 0;
549}
550
551/*
552 * must get all pending data because we only get an irq on empty->non-empty
553 * transition
554 */
555static irqreturn_t hvsi_interrupt(int irq, void *arg)
556{
557	struct hvsi_struct *hp = (struct hvsi_struct *)arg;
558	struct tty_struct *flip;
559	struct tty_struct *hangup;
560	struct hvsi_struct *handshake;
561	unsigned long flags;
562	int again = 1;
563
564	pr_debug("%s\n", __func__);
565
566	while (again) {
567		spin_lock_irqsave(&hp->lock, flags);
568		again = hvsi_load_chunk(hp, &flip, &hangup, &handshake);
569		spin_unlock_irqrestore(&hp->lock, flags);
570
571		/*
572		 * we have to call tty_flip_buffer_push() and tty_hangup() outside our
573		 * spinlock. But we also have to keep going until we've read all the
574		 * available data.
575		 */
576
577		if (flip) {
578			/* there was data put in the tty flip buffer */
579			tty_flip_buffer_push(flip);
580			flip = NULL;
581		}
582
583		if (hangup) {
584			tty_hangup(hangup);
585		}
586
587		if (handshake) {
588			pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
589			schedule_work(&handshake->handshaker);
590		}
591	}
592
593	spin_lock_irqsave(&hp->lock, flags);
594	if (hp->tty && hp->n_throttle
595			&& (!test_bit(TTY_THROTTLED, &hp->tty->flags))) {
596		/* we weren't hung up and we weren't throttled, so we can deliver the
597		 * rest now */
598		flip = hp->tty;
599		hvsi_send_overflow(hp);
600	}
601	spin_unlock_irqrestore(&hp->lock, flags);
602
603	if (flip) {
604		tty_flip_buffer_push(flip);
605	}
606
607	return IRQ_HANDLED;
608}
609
610/* for boot console, before the irq handler is running */
611static int __init poll_for_state(struct hvsi_struct *hp, int state)
612{
613	unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
614
615	for (;;) {
616		hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
617
618		if (hp->state == state)
619			return 0;
620
621		mdelay(5);
622		if (time_after(jiffies, end_jiffies))
623			return -EIO;
624	}
625}
626
627/* wait for irq handler to change our state */
628static int wait_for_state(struct hvsi_struct *hp, int state)
629{
630	int ret = 0;
631
632	if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
633		ret = -EIO;
634
635	return ret;
636}
637
638static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
639{
640	struct hvsi_query packet __ALIGNED__;
641	int wrote;
642
643	packet.type = VS_QUERY_PACKET_HEADER;
644	packet.len = sizeof(struct hvsi_query);
645	packet.seqno = atomic_inc_return(&hp->seqno);
646	packet.verb = verb;
647
648	pr_debug("%s: sending %i bytes\n", __func__, packet.len);
649	dbg_dump_hex((uint8_t*)&packet, packet.len);
650
651	wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
652	if (wrote != packet.len) {
653		printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
654			wrote);
655		return -EIO;
656	}
657
658	return 0;
659}
660
661static int hvsi_get_mctrl(struct hvsi_struct *hp)
662{
663	int ret;
664
665	set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
666	hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
667
668	ret = hvsi_wait(hp, HVSI_OPEN);
669	if (ret < 0) {
670		printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
671		set_state(hp, HVSI_OPEN);
672		return ret;
673	}
674
675	pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
676
677	return 0;
678}
679
680/* note that we can only set DTR */
681static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
682{
683	struct hvsi_control packet __ALIGNED__;
684	int wrote;
685
686	packet.type = VS_CONTROL_PACKET_HEADER,
687	packet.seqno = atomic_inc_return(&hp->seqno);
688	packet.len = sizeof(struct hvsi_control);
689	packet.verb = VSV_SET_MODEM_CTL;
690	packet.mask = HVSI_TSDTR;
691
692	if (mctrl & TIOCM_DTR)
693		packet.word = HVSI_TSDTR;
694
695	pr_debug("%s: sending %i bytes\n", __func__, packet.len);
696	dbg_dump_hex((uint8_t*)&packet, packet.len);
697
698	wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
699	if (wrote != packet.len) {
700		printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
701		return -EIO;
702	}
703
704	return 0;
705}
706
707static void hvsi_drain_input(struct hvsi_struct *hp)
708{
709	uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
710	unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
711
712	while (time_before(end_jiffies, jiffies))
713		if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
714			break;
715}
716
717static int hvsi_handshake(struct hvsi_struct *hp)
718{
719	int ret;
720
721	/*
722	 * We could have a CLOSE or other data waiting for us before we even try
723	 * to open; try to throw it all away so we don't get confused. (CLOSE
724	 * is the first message sent up the pipe when the FSP comes online. We
725	 * need to distinguish between "it came up a while ago and we're the first
726	 * user" and "it was just reset before it saw our handshake packet".)
727	 */
728	hvsi_drain_input(hp);
729
730	set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
731	ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
732	if (ret < 0) {
733		printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
734		return ret;
735	}
736
737	ret = hvsi_wait(hp, HVSI_OPEN);
738	if (ret < 0)
739		return ret;
740
741	return 0;
742}
743
744static void hvsi_handshaker(struct work_struct *work)
745{
746	struct hvsi_struct *hp =
747		container_of(work, struct hvsi_struct, handshaker);
748
749	if (hvsi_handshake(hp) >= 0)
750		return;
751
752	printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
753	if (is_console(hp)) {
754		/*
755		 * ttys will re-attempt the handshake via hvsi_open, but
756		 * the console will not.
757		 */
758		printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
759	}
760}
761
762static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
763{
764	struct hvsi_data packet __ALIGNED__;
765	int ret;
766
767	BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
768
769	packet.type = VS_DATA_PACKET_HEADER;
770	packet.seqno = atomic_inc_return(&hp->seqno);
771	packet.len = count + sizeof(struct hvsi_header);
772	memcpy(&packet.data, buf, count);
773
774	ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
775	if (ret == packet.len) {
776		/* return the number of chars written, not the packet length */
777		return count;
778	}
779	return ret; /* return any errors */
780}
781
782static void hvsi_close_protocol(struct hvsi_struct *hp)
783{
784	struct hvsi_control packet __ALIGNED__;
785
786	packet.type = VS_CONTROL_PACKET_HEADER;
787	packet.seqno = atomic_inc_return(&hp->seqno);
788	packet.len = 6;
789	packet.verb = VSV_CLOSE_PROTOCOL;
790
791	pr_debug("%s: sending %i bytes\n", __func__, packet.len);
792	dbg_dump_hex((uint8_t*)&packet, packet.len);
793
794	hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
795}
796
797static int hvsi_open(struct tty_struct *tty, struct file *filp)
798{
799	struct hvsi_struct *hp;
800	unsigned long flags;
801	int line = tty->index;
802	int ret;
803
804	pr_debug("%s\n", __func__);
805
806	if (line < 0 || line >= hvsi_count)
807		return -ENODEV;
808	hp = &hvsi_ports[line];
809
810	tty->driver_data = hp;
811
812	mb();
813	if (hp->state == HVSI_FSP_DIED)
814		return -EIO;
815
816	spin_lock_irqsave(&hp->lock, flags);
817	hp->tty = tty;
818	hp->count++;
819	atomic_set(&hp->seqno, 0);
820	h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
821	spin_unlock_irqrestore(&hp->lock, flags);
822
823	if (is_console(hp))
824		return 0; /* this has already been handshaked as the console */
825
826	ret = hvsi_handshake(hp);
827	if (ret < 0) {
828		printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
829		return ret;
830	}
831
832	ret = hvsi_get_mctrl(hp);
833	if (ret < 0) {
834		printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
835		return ret;
836	}
837
838	ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
839	if (ret < 0) {
840		printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
841		return ret;
842	}
843
844	return 0;
845}
846
847/* wait for hvsi_write_worker to empty hp->outbuf */
848static void hvsi_flush_output(struct hvsi_struct *hp)
849{
850	wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
851
852	/* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
853	cancel_delayed_work(&hp->writer);
854	flush_scheduled_work();
855
856	/*
857	 * it's also possible that our timeout expired and hvsi_write_worker
858	 * didn't manage to push outbuf. poof.
859	 */
860	hp->n_outbuf = 0;
861}
862
863static void hvsi_close(struct tty_struct *tty, struct file *filp)
864{
865	struct hvsi_struct *hp = tty->driver_data;
866	unsigned long flags;
867
868	pr_debug("%s\n", __func__);
869
870	if (tty_hung_up_p(filp))
871		return;
872
873	spin_lock_irqsave(&hp->lock, flags);
874
875	if (--hp->count == 0) {
876		hp->tty = NULL;
877		hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
878
879		/* only close down connection if it is not the console */
880		if (!is_console(hp)) {
881			h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
882			__set_state(hp, HVSI_CLOSED);
883			/*
884			 * any data delivered to the tty layer after this will be
885			 * discarded (except for XON/XOFF)
886			 */
887			tty->closing = 1;
888
889			spin_unlock_irqrestore(&hp->lock, flags);
890
891			/* let any existing irq handlers finish. no more will start. */
892			synchronize_irq(hp->virq);
893
894			/* hvsi_write_worker will re-schedule until outbuf is empty. */
895			hvsi_flush_output(hp);
896
897			/* tell FSP to stop sending data */
898			hvsi_close_protocol(hp);
899
900			/*
901			 * drain anything FSP is still in the middle of sending, and let
902			 * hvsi_handshake drain the rest on the next open.
903			 */
904			hvsi_drain_input(hp);
905
906			spin_lock_irqsave(&hp->lock, flags);
907		}
908	} else if (hp->count < 0)
909		printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
910		       hp - hvsi_ports, hp->count);
911
912	spin_unlock_irqrestore(&hp->lock, flags);
913}
914
915static void hvsi_hangup(struct tty_struct *tty)
916{
917	struct hvsi_struct *hp = tty->driver_data;
918	unsigned long flags;
919
920	pr_debug("%s\n", __func__);
921
922	spin_lock_irqsave(&hp->lock, flags);
923
924	hp->count = 0;
925	hp->n_outbuf = 0;
926	hp->tty = NULL;
927
928	spin_unlock_irqrestore(&hp->lock, flags);
929}
930
931/* called with hp->lock held */
932static void hvsi_push(struct hvsi_struct *hp)
933{
934	int n;
935
936	if (hp->n_outbuf <= 0)
937		return;
938
939	n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
940	if (n > 0) {
941		/* success */
942		pr_debug("%s: wrote %i chars\n", __func__, n);
943		hp->n_outbuf = 0;
944	} else if (n == -EIO) {
945		__set_state(hp, HVSI_FSP_DIED);
946		printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
947	}
948}
949
950/* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
951static void hvsi_write_worker(struct work_struct *work)
952{
953	struct hvsi_struct *hp =
954		container_of(work, struct hvsi_struct, writer.work);
955	unsigned long flags;
956#ifdef DEBUG
957	static long start_j = 0;
958
959	if (start_j == 0)
960		start_j = jiffies;
961#endif /* DEBUG */
962
963	spin_lock_irqsave(&hp->lock, flags);
964
965	pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
966
967	if (!is_open(hp)) {
968		/*
969		 * We could have a non-open connection if the service processor died
970		 * while we were busily scheduling ourselves. In that case, it could
971		 * be minutes before the service processor comes back, so only try
972		 * again once a second.
973		 */
974		schedule_delayed_work(&hp->writer, HZ);
975		goto out;
976	}
977
978	hvsi_push(hp);
979	if (hp->n_outbuf > 0)
980		schedule_delayed_work(&hp->writer, 10);
981	else {
982#ifdef DEBUG
983		pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
984				jiffies - start_j);
985		start_j = 0;
986#endif /* DEBUG */
987		wake_up_all(&hp->emptyq);
988		tty_wakeup(hp->tty);
989	}
990
991out:
992	spin_unlock_irqrestore(&hp->lock, flags);
993}
994
995static int hvsi_write_room(struct tty_struct *tty)
996{
997	struct hvsi_struct *hp = tty->driver_data;
998
999	return N_OUTBUF - hp->n_outbuf;
1000}
1001
1002static int hvsi_chars_in_buffer(struct tty_struct *tty)
1003{
1004	struct hvsi_struct *hp = tty->driver_data;
1005
1006	return hp->n_outbuf;
1007}
1008
1009static int hvsi_write(struct tty_struct *tty,
1010		     const unsigned char *buf, int count)
1011{
1012	struct hvsi_struct *hp = tty->driver_data;
1013	const char *source = buf;
1014	unsigned long flags;
1015	int total = 0;
1016	int origcount = count;
1017
1018	spin_lock_irqsave(&hp->lock, flags);
1019
1020	pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
1021
1022	if (!is_open(hp)) {
1023		/* we're either closing or not yet open; don't accept data */
1024		pr_debug("%s: not open\n", __func__);
1025		goto out;
1026	}
1027
1028	/*
1029	 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
1030	 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
1031	 * will see there is no room in outbuf and return.
1032	 */
1033	while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) {
1034		int chunksize = min(count, hvsi_write_room(hp->tty));
1035
1036		BUG_ON(hp->n_outbuf < 0);
1037		memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
1038		hp->n_outbuf += chunksize;
1039
1040		total += chunksize;
1041		source += chunksize;
1042		count -= chunksize;
1043		hvsi_push(hp);
1044	}
1045
1046	if (hp->n_outbuf > 0) {
1047		/*
1048		 * we weren't able to write it all to the hypervisor.
1049		 * schedule another push attempt.
1050		 */
1051		schedule_delayed_work(&hp->writer, 10);
1052	}
1053
1054out:
1055	spin_unlock_irqrestore(&hp->lock, flags);
1056
1057	if (total != origcount)
1058		pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
1059			total);
1060
1061	return total;
1062}
1063
1064/*
1065 * I have never seen throttle or unthrottle called, so this little throttle
1066 * buffering scheme may or may not work.
1067 */
1068static void hvsi_throttle(struct tty_struct *tty)
1069{
1070	struct hvsi_struct *hp = tty->driver_data;
1071
1072	pr_debug("%s\n", __func__);
1073
1074	h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
1075}
1076
1077static void hvsi_unthrottle(struct tty_struct *tty)
1078{
1079	struct hvsi_struct *hp = tty->driver_data;
1080	unsigned long flags;
1081	int shouldflip = 0;
1082
1083	pr_debug("%s\n", __func__);
1084
1085	spin_lock_irqsave(&hp->lock, flags);
1086	if (hp->n_throttle) {
1087		hvsi_send_overflow(hp);
1088		shouldflip = 1;
1089	}
1090	spin_unlock_irqrestore(&hp->lock, flags);
1091
1092	if (shouldflip)
1093		tty_flip_buffer_push(hp->tty);
1094
1095	h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1096}
1097
1098static int hvsi_tiocmget(struct tty_struct *tty, struct file *file)
1099{
1100	struct hvsi_struct *hp = tty->driver_data;
1101
1102	hvsi_get_mctrl(hp);
1103	return hp->mctrl;
1104}
1105
1106static int hvsi_tiocmset(struct tty_struct *tty, struct file *file,
1107		unsigned int set, unsigned int clear)
1108{
1109	struct hvsi_struct *hp = tty->driver_data;
1110	unsigned long flags;
1111	uint16_t new_mctrl;
1112
1113	/* we can only alter DTR */
1114	clear &= TIOCM_DTR;
1115	set &= TIOCM_DTR;
1116
1117	spin_lock_irqsave(&hp->lock, flags);
1118
1119	new_mctrl = (hp->mctrl & ~clear) | set;
1120
1121	if (hp->mctrl != new_mctrl) {
1122		hvsi_set_mctrl(hp, new_mctrl);
1123		hp->mctrl = new_mctrl;
1124	}
1125	spin_unlock_irqrestore(&hp->lock, flags);
1126
1127	return 0;
1128}
1129
1130
1131static const struct tty_operations hvsi_ops = {
1132	.open = hvsi_open,
1133	.close = hvsi_close,
1134	.write = hvsi_write,
1135	.hangup = hvsi_hangup,
1136	.write_room = hvsi_write_room,
1137	.chars_in_buffer = hvsi_chars_in_buffer,
1138	.throttle = hvsi_throttle,
1139	.unthrottle = hvsi_unthrottle,
1140	.tiocmget = hvsi_tiocmget,
1141	.tiocmset = hvsi_tiocmset,
1142};
1143
1144static int __init hvsi_init(void)
1145{
1146	int i;
1147
1148	hvsi_driver = alloc_tty_driver(hvsi_count);
1149	if (!hvsi_driver)
1150		return -ENOMEM;
1151
1152	hvsi_driver->owner = THIS_MODULE;
1153	hvsi_driver->driver_name = "hvsi";
1154	hvsi_driver->name = "hvsi";
1155	hvsi_driver->major = HVSI_MAJOR;
1156	hvsi_driver->minor_start = HVSI_MINOR;
1157	hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1158	hvsi_driver->init_termios = tty_std_termios;
1159	hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1160	hvsi_driver->init_termios.c_ispeed = 9600;
1161	hvsi_driver->init_termios.c_ospeed = 9600;
1162	hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1163	tty_set_operations(hvsi_driver, &hvsi_ops);
1164
1165	for (i=0; i < hvsi_count; i++) {
1166		struct hvsi_struct *hp = &hvsi_ports[i];
1167		int ret = 1;
1168
1169		ret = request_irq(hp->virq, hvsi_interrupt, IRQF_DISABLED, "hvsi", hp);
1170		if (ret)
1171			printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1172				hp->virq, ret);
1173	}
1174	hvsi_wait = wait_for_state; /* irqs active now */
1175
1176	if (tty_register_driver(hvsi_driver))
1177		panic("Couldn't register hvsi console driver\n");
1178
1179	printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1180
1181	return 0;
1182}
1183device_initcall(hvsi_init);
1184
1185/***** console (not tty) code: *****/
1186
1187static void hvsi_console_print(struct console *console, const char *buf,
1188		unsigned int count)
1189{
1190	struct hvsi_struct *hp = &hvsi_ports[console->index];
1191	char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1192	unsigned int i = 0, n = 0;
1193	int ret, donecr = 0;
1194
1195	mb();
1196	if (!is_open(hp))
1197		return;
1198
1199	/*
1200	 * ugh, we have to translate LF -> CRLF ourselves, in place.
1201	 * copied from hvc_console.c:
1202	 */
1203	while (count > 0 || i > 0) {
1204		if (count > 0 && i < sizeof(c)) {
1205			if (buf[n] == '\n' && !donecr) {
1206				c[i++] = '\r';
1207				donecr = 1;
1208			} else {
1209				c[i++] = buf[n++];
1210				donecr = 0;
1211				--count;
1212			}
1213		} else {
1214			ret = hvsi_put_chars(hp, c, i);
1215			if (ret < 0)
1216				i = 0;
1217			i -= ret;
1218		}
1219	}
1220}
1221
1222static struct tty_driver *hvsi_console_device(struct console *console,
1223	int *index)
1224{
1225	*index = console->index;
1226	return hvsi_driver;
1227}
1228
1229static int __init hvsi_console_setup(struct console *console, char *options)
1230{
1231	struct hvsi_struct *hp;
1232	int ret;
1233
1234	if (console->index < 0 || console->index >= hvsi_count)
1235		return -1;
1236	hp = &hvsi_ports[console->index];
1237
1238	/* give the FSP a chance to change the baud rate when we re-open */
1239	hvsi_close_protocol(hp);
1240
1241	ret = hvsi_handshake(hp);
1242	if (ret < 0)
1243		return ret;
1244
1245	ret = hvsi_get_mctrl(hp);
1246	if (ret < 0)
1247		return ret;
1248
1249	ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1250	if (ret < 0)
1251		return ret;
1252
1253	hp->flags |= HVSI_CONSOLE;
1254
1255	return 0;
1256}
1257
1258static struct console hvsi_console = {
1259	.name		= "hvsi",
1260	.write		= hvsi_console_print,
1261	.device		= hvsi_console_device,
1262	.setup		= hvsi_console_setup,
1263	.flags		= CON_PRINTBUFFER,
1264	.index		= -1,
1265};
1266
1267static int __init hvsi_console_init(void)
1268{
1269	struct device_node *vty;
1270
1271	hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1272
1273	/* search device tree for vty nodes */
1274	for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol");
1275			vty != NULL;
1276			vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) {
1277		struct hvsi_struct *hp;
1278		const uint32_t *vtermno, *irq;
1279
1280		vtermno = of_get_property(vty, "reg", NULL);
1281		irq = of_get_property(vty, "interrupts", NULL);
1282		if (!vtermno || !irq)
1283			continue;
1284
1285		if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1286			of_node_put(vty);
1287			break;
1288		}
1289
1290		hp = &hvsi_ports[hvsi_count];
1291		INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1292		INIT_WORK(&hp->handshaker, hvsi_handshaker);
1293		init_waitqueue_head(&hp->emptyq);
1294		init_waitqueue_head(&hp->stateq);
1295		spin_lock_init(&hp->lock);
1296		hp->index = hvsi_count;
1297		hp->inbuf_end = hp->inbuf;
1298		hp->state = HVSI_CLOSED;
1299		hp->vtermno = *vtermno;
1300		hp->virq = irq_create_mapping(NULL, irq[0]);
1301		if (hp->virq == NO_IRQ) {
1302			printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
1303				__func__, irq[0]);
1304			continue;
1305		}
1306
1307		hvsi_count++;
1308	}
1309
1310	if (hvsi_count)
1311		register_console(&hvsi_console);
1312	return 0;
1313}
1314console_initcall(hvsi_console_init);
1315