blob: a7488b7486473c8139d8baae1874ac86bf7979ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#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>
Benjamin Herrenschmidt725e7892011-06-16 15:08:12 +000052#include <asm/hvsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define HVSI_MAJOR 229
55#define HVSI_MINOR 128
56#define MAX_NR_HVSI_CONSOLES 4
57
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
63#define N_OUTBUF 12
64
65/*
66 * we pass data via two 8-byte registers, so we would like our char arrays
67 * properly aligned for those loads.
68 */
69#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
70
71struct hvsi_struct {
David Howellsc4028952006-11-22 14:57:56 +000072 struct delayed_work writer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 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 */
76 spinlock_t lock;
77 int index;
78 struct tty_struct *tty;
roel kluin3d268252008-12-02 11:21:43 +000079 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 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];
84 uint8_t *inbuf_end;
85 int n_throttle;
86 int n_outbuf;
87 uint32_t vtermno;
88 uint32_t virq;
89 atomic_t seqno; /* HVSI packet sequence number */
90 uint16_t mctrl;
91 uint8_t state; /* HVSI protocol state */
92 uint8_t flags;
93#ifdef CONFIG_MAGIC_SYSRQ
94 uint8_t sysrq;
95#endif /* CONFIG_MAGIC_SYSRQ */
96};
97static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
98
99static struct tty_driver *hvsi_driver;
100static int hvsi_count;
101static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
102
103enum HVSI_PROTOCOL_STATE {
104 HVSI_CLOSED,
105 HVSI_WAIT_FOR_VER_RESPONSE,
106 HVSI_WAIT_FOR_VER_QUERY,
107 HVSI_OPEN,
108 HVSI_WAIT_FOR_MCTRL_RESPONSE,
109 HVSI_FSP_DIED,
110};
111#define HVSI_CONSOLE 0x1
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static inline int is_console(struct hvsi_struct *hp)
114{
115 return hp->flags & HVSI_CONSOLE;
116}
117
118static inline int is_open(struct hvsi_struct *hp)
119{
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);
123}
124
125static inline void print_state(struct hvsi_struct *hp)
126{
127#ifdef DEBUG
128 static const char *state_names[] = {
129 "HVSI_CLOSED",
130 "HVSI_WAIT_FOR_VER_RESPONSE",
131 "HVSI_WAIT_FOR_VER_QUERY",
132 "HVSI_OPEN",
133 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
134 "HVSI_FSP_DIED",
135 };
Phil Carmody08a82c62010-05-24 14:33:04 -0700136 const char *name = (hp->state < ARRAY_SIZE(state_names))
137 ? state_names[hp->state] : "UNKNOWN";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 pr_debug("hvsi%i: state = %s\n", hp->index, name);
140#endif /* DEBUG */
141}
142
143static inline void __set_state(struct hvsi_struct *hp, int state)
144{
145 hp->state = state;
146 print_state(hp);
147 wake_up_all(&hp->stateq);
148}
149
150static inline void set_state(struct hvsi_struct *hp, int state)
151{
152 unsigned long flags;
153
154 spin_lock_irqsave(&hp->lock, flags);
155 __set_state(hp, state);
156 spin_unlock_irqrestore(&hp->lock, flags);
157}
158
159static inline int len_packet(const uint8_t *packet)
160{
161 return (int)((struct hvsi_header *)packet)->len;
162}
163
164static inline int is_header(const uint8_t *packet)
165{
166 struct hvsi_header *header = (struct hvsi_header *)packet;
167 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
168}
169
170static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
171{
172 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
173 return 0; /* don't even have the packet header */
174
175 if (hp->inbuf_end < (packet + len_packet(packet)))
176 return 0; /* don't have the rest of the packet */
177
178 return 1;
179}
180
181/* shift remaining bytes in packetbuf down */
182static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
183{
184 int remaining = (int)(hp->inbuf_end - read_to);
185
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700186 pr_debug("%s: %i chars remain\n", __func__, remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (read_to != hp->inbuf)
189 memmove(hp->inbuf, read_to, remaining);
190
191 hp->inbuf_end = hp->inbuf + remaining;
192}
193
194#ifdef DEBUG
195#define dbg_dump_packet(packet) dump_packet(packet)
196#define dbg_dump_hex(data, len) dump_hex(data, len)
197#else
198#define dbg_dump_packet(packet) do { } while (0)
199#define dbg_dump_hex(data, len) do { } while (0)
200#endif
201
202static void dump_hex(const uint8_t *data, int len)
203{
204 int i;
205
206 printk(" ");
207 for (i=0; i < len; i++)
208 printk("%.2x", data[i]);
209
210 printk("\n ");
211 for (i=0; i < len; i++) {
212 if (isprint(data[i]))
213 printk("%c", data[i]);
214 else
215 printk(".");
216 }
217 printk("\n");
218}
219
220static void dump_packet(uint8_t *packet)
221{
222 struct hvsi_header *header = (struct hvsi_header *)packet;
223
224 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
225 header->seqno);
226
227 dump_hex(packet, header->len);
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
231{
232 unsigned long got;
233
Milton Miller88de0be2005-07-07 17:56:27 -0700234 got = hvc_get_chars(hp->vtermno, buf, count);
235
236 return got;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
240 struct tty_struct **to_hangup, struct hvsi_struct **to_handshake)
241{
242 struct hvsi_control *header = (struct hvsi_control *)packet;
243
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;
Linas Vepstas229395c2006-08-05 12:14:36 -0700250 /* If userland hasn't done an open(2) yet, hp->tty is NULL. */
251 if (hp->tty && !(hp->tty->flags & CLOCAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *to_hangup = hp->tty;
253 }
254 break;
255 case VSV_CLOSE_PROTOCOL:
256 pr_debug("hvsi%i: service processor came back\n", hp->index);
257 if (hp->state != HVSI_CLOSED) {
258 *to_handshake = hp;
259 }
260 break;
261 default:
262 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
263 hp->index);
264 dump_packet(packet);
265 break;
266 }
267}
268
269static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
270{
271 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
272
273 switch (hp->state) {
274 case HVSI_WAIT_FOR_VER_RESPONSE:
275 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
276 break;
277 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
278 hp->mctrl = 0;
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);
284 break;
285 default:
286 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
287 dump_packet(packet);
288 break;
289 }
290}
291
292/* respond to service processor's version query */
293static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
294{
295 struct hvsi_query_response packet __ALIGNED__;
296 int wrote;
297
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000298 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 packet.verb = VSV_SEND_VERSION_NUMBER;
302 packet.u.version = HVSI_VERSION;
303 packet.query_seqno = query_seqno+1;
304
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000305 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
306 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000308 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
309 if (wrote != packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
311 hp->index);
312 return -EIO;
313 }
314
315 return 0;
316}
317
318static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
319{
320 struct hvsi_query *query = (struct hvsi_query *)packet;
321
322 switch (hp->state) {
323 case HVSI_WAIT_FOR_VER_QUERY:
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000324 hvsi_version_respond(hp, query->hdr.seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 __set_state(hp, HVSI_OPEN);
326 break;
327 default:
328 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
329 dump_packet(packet);
330 break;
331 }
332}
333
334static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
335{
336 int i;
337
338 for (i=0; i < len; i++) {
339 char c = buf[i];
340#ifdef CONFIG_MAGIC_SYSRQ
341 if (c == '\0') {
342 hp->sysrq = 1;
343 continue;
344 } else if (hp->sysrq) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700345 handle_sysrq(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 hp->sysrq = 0;
347 continue;
348 }
349#endif /* CONFIG_MAGIC_SYSRQ */
350 tty_insert_flip_char(hp->tty, c, 0);
351 }
352}
353
354/*
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
360 * revisited.
361 */
362#define TTY_THRESHOLD_THROTTLE 128
363static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
364 const uint8_t *packet)
365{
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;
370
371 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
372
373 if (datalen == 0)
374 return NULL;
375
376 if (overflow > 0) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700377 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 datalen = TTY_THRESHOLD_THROTTLE;
379 }
380
381 hvsi_insert_chars(hp, data, datalen);
382
383 if (overflow > 0) {
384 /*
385 * we still have more data to deliver, so we need to save off the
386 * overflow and send it later
387 */
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700388 pr_debug("%s: deferring overflow\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
390 hp->n_throttle = overflow;
391 }
392
393 return hp->tty;
394}
395
396/*
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
400 * incoming data).
401 */
402static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
403 struct tty_struct **hangup, struct hvsi_struct **handshake)
404{
405 uint8_t *packet = hp->inbuf;
406 int chunklen;
407
408 *flip = NULL;
409 *hangup = NULL;
410 *handshake = NULL;
411
412 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
413 if (chunklen == 0) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700414 pr_debug("%s: 0-length read\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
416 }
417
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700418 pr_debug("%s: got %i bytes\n", __func__, chunklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dbg_dump_hex(hp->inbuf_end, chunklen);
420
421 hp->inbuf_end += chunklen;
422
423 /* handle all completed packets */
424 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
425 struct hvsi_header *header = (struct hvsi_header *)packet;
426
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)))
431 packet++;
432 continue;
433 }
434
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700435 pr_debug("%s: handling %i-byte packet\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 len_packet(packet));
437 dbg_dump_packet(packet);
438
439 switch (header->type) {
440 case VS_DATA_PACKET_HEADER:
441 if (!is_open(hp))
442 break;
443 if (hp->tty == NULL)
444 break; /* no tty buffer to put data in */
445 *flip = hvsi_recv_data(hp, packet);
446 break;
447 case VS_CONTROL_PACKET_HEADER:
448 hvsi_recv_control(hp, packet, hangup, handshake);
449 break;
450 case VS_QUERY_RESPONSE_PACKET_HEADER:
451 hvsi_recv_response(hp, packet);
452 break;
453 case VS_QUERY_PACKET_HEADER:
454 hvsi_recv_query(hp, packet);
455 break;
456 default:
457 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
458 hp->index, header->type);
459 dump_packet(packet);
460 break;
461 }
462
463 packet += len_packet(packet);
464
465 if (*hangup || *handshake) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700466 pr_debug("%s: hangup or handshake\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
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.
471 */
472 break;
473 }
474 }
475
476 compact_inbuf(hp, packet);
477
478 return 1;
479}
480
481static void hvsi_send_overflow(struct hvsi_struct *hp)
482{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700483 pr_debug("%s: delivering %i bytes overflow\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 hp->n_throttle);
485
486 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
487 hp->n_throttle = 0;
488}
489
490/*
491 * must get all pending data because we only get an irq on empty->non-empty
492 * transition
493 */
David Howells7d12e782006-10-05 14:55:46 +0100494static irqreturn_t hvsi_interrupt(int irq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
497 struct tty_struct *flip;
498 struct tty_struct *hangup;
499 struct hvsi_struct *handshake;
500 unsigned long flags;
501 int again = 1;
502
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700503 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 while (again) {
506 spin_lock_irqsave(&hp->lock, flags);
507 again = hvsi_load_chunk(hp, &flip, &hangup, &handshake);
508 spin_unlock_irqrestore(&hp->lock, flags);
509
510 /*
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
513 * available data.
514 */
515
516 if (flip) {
517 /* there was data put in the tty flip buffer */
518 tty_flip_buffer_push(flip);
519 flip = NULL;
520 }
521
522 if (hangup) {
523 tty_hangup(hangup);
524 }
525
526 if (handshake) {
527 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
528 schedule_work(&handshake->handshaker);
529 }
530 }
531
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
536 * rest now */
537 flip = hp->tty;
538 hvsi_send_overflow(hp);
539 }
540 spin_unlock_irqrestore(&hp->lock, flags);
541
542 if (flip) {
543 tty_flip_buffer_push(flip);
544 }
545
546 return IRQ_HANDLED;
547}
548
549/* for boot console, before the irq handler is running */
550static int __init poll_for_state(struct hvsi_struct *hp, int state)
551{
552 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
553
554 for (;;) {
David Howells7d12e782006-10-05 14:55:46 +0100555 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (hp->state == state)
558 return 0;
559
560 mdelay(5);
561 if (time_after(jiffies, end_jiffies))
562 return -EIO;
563 }
564}
565
566/* wait for irq handler to change our state */
567static int wait_for_state(struct hvsi_struct *hp, int state)
568{
569 int ret = 0;
570
571 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
572 ret = -EIO;
573
574 return ret;
575}
576
577static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
578{
579 struct hvsi_query packet __ALIGNED__;
580 int wrote;
581
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000582 packet.hdr.type = VS_QUERY_PACKET_HEADER;
583 packet.hdr.len = sizeof(struct hvsi_query);
584 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 packet.verb = verb;
586
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000587 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
588 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000590 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
591 if (wrote != packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
593 wrote);
594 return -EIO;
595 }
596
597 return 0;
598}
599
600static int hvsi_get_mctrl(struct hvsi_struct *hp)
601{
602 int ret;
603
604 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
605 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
606
607 ret = hvsi_wait(hp, HVSI_OPEN);
608 if (ret < 0) {
609 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
610 set_state(hp, HVSI_OPEN);
611 return ret;
612 }
613
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700614 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 return 0;
617}
618
619/* note that we can only set DTR */
620static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
621{
622 struct hvsi_control packet __ALIGNED__;
623 int wrote;
624
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000625 packet.hdr.type = VS_CONTROL_PACKET_HEADER,
626 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
627 packet.hdr.len = sizeof(struct hvsi_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 packet.verb = VSV_SET_MODEM_CTL;
629 packet.mask = HVSI_TSDTR;
630
631 if (mctrl & TIOCM_DTR)
632 packet.word = HVSI_TSDTR;
633
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000634 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
635 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000637 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
638 if (wrote != packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
640 return -EIO;
641 }
642
643 return 0;
644}
645
646static void hvsi_drain_input(struct hvsi_struct *hp)
647{
648 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
649 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
650
651 while (time_before(end_jiffies, jiffies))
652 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
653 break;
654}
655
656static int hvsi_handshake(struct hvsi_struct *hp)
657{
658 int ret;
659
660 /*
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".)
666 */
667 hvsi_drain_input(hp);
668
669 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
670 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
671 if (ret < 0) {
672 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
673 return ret;
674 }
675
676 ret = hvsi_wait(hp, HVSI_OPEN);
677 if (ret < 0)
678 return ret;
679
680 return 0;
681}
682
David Howellsc4028952006-11-22 14:57:56 +0000683static void hvsi_handshaker(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
David Howellsc4028952006-11-22 14:57:56 +0000685 struct hvsi_struct *hp =
686 container_of(work, struct hvsi_struct, handshaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (hvsi_handshake(hp) >= 0)
689 return;
690
691 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
692 if (is_console(hp)) {
693 /*
694 * ttys will re-attempt the handshake via hvsi_open, but
695 * the console will not.
696 */
697 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
698 }
699}
700
701static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
702{
703 struct hvsi_data packet __ALIGNED__;
704 int ret;
705
706 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
707
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000708 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 memcpy(&packet.data, buf, count);
712
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000713 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
714 if (ret == packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* return the number of chars written, not the packet length */
716 return count;
717 }
718 return ret; /* return any errors */
719}
720
721static void hvsi_close_protocol(struct hvsi_struct *hp)
722{
723 struct hvsi_control packet __ALIGNED__;
724
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000725 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
726 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
727 packet.hdr.len = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 packet.verb = VSV_CLOSE_PROTOCOL;
729
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000730 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
731 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000733 hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736static int hvsi_open(struct tty_struct *tty, struct file *filp)
737{
738 struct hvsi_struct *hp;
739 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int ret;
741
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700742 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Jiri Slaby410235f2012-03-05 14:52:01 +0100744 hp = &hvsi_ports[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 tty->driver_data = hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 mb();
749 if (hp->state == HVSI_FSP_DIED)
750 return -EIO;
751
752 spin_lock_irqsave(&hp->lock, flags);
753 hp->tty = tty;
754 hp->count++;
755 atomic_set(&hp->seqno, 0);
756 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
757 spin_unlock_irqrestore(&hp->lock, flags);
758
759 if (is_console(hp))
760 return 0; /* this has already been handshaked as the console */
761
762 ret = hvsi_handshake(hp);
763 if (ret < 0) {
764 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
765 return ret;
766 }
767
768 ret = hvsi_get_mctrl(hp);
769 if (ret < 0) {
770 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
771 return ret;
772 }
773
774 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
775 if (ret < 0) {
776 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
777 return ret;
778 }
779
780 return 0;
781}
782
783/* wait for hvsi_write_worker to empty hp->outbuf */
784static void hvsi_flush_output(struct hvsi_struct *hp)
785{
786 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
787
788 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
Tejun Heo42565992010-12-24 15:59:07 +0100789 cancel_delayed_work_sync(&hp->writer);
790 flush_work_sync(&hp->handshaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /*
793 * it's also possible that our timeout expired and hvsi_write_worker
794 * didn't manage to push outbuf. poof.
795 */
796 hp->n_outbuf = 0;
797}
798
799static void hvsi_close(struct tty_struct *tty, struct file *filp)
800{
801 struct hvsi_struct *hp = tty->driver_data;
802 unsigned long flags;
803
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700804 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (tty_hung_up_p(filp))
807 return;
808
809 spin_lock_irqsave(&hp->lock, flags);
810
811 if (--hp->count == 0) {
812 hp->tty = NULL;
813 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
814
815 /* only close down connection if it is not the console */
816 if (!is_console(hp)) {
817 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
818 __set_state(hp, HVSI_CLOSED);
819 /*
820 * any data delivered to the tty layer after this will be
821 * discarded (except for XON/XOFF)
822 */
823 tty->closing = 1;
824
825 spin_unlock_irqrestore(&hp->lock, flags);
826
827 /* let any existing irq handlers finish. no more will start. */
828 synchronize_irq(hp->virq);
829
830 /* hvsi_write_worker will re-schedule until outbuf is empty. */
831 hvsi_flush_output(hp);
832
833 /* tell FSP to stop sending data */
834 hvsi_close_protocol(hp);
835
836 /*
837 * drain anything FSP is still in the middle of sending, and let
838 * hvsi_handshake drain the rest on the next open.
839 */
840 hvsi_drain_input(hp);
841
842 spin_lock_irqsave(&hp->lock, flags);
843 }
844 } else if (hp->count < 0)
845 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
846 hp - hvsi_ports, hp->count);
847
848 spin_unlock_irqrestore(&hp->lock, flags);
849}
850
851static void hvsi_hangup(struct tty_struct *tty)
852{
853 struct hvsi_struct *hp = tty->driver_data;
854 unsigned long flags;
855
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700856 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 spin_lock_irqsave(&hp->lock, flags);
859
860 hp->count = 0;
861 hp->n_outbuf = 0;
862 hp->tty = NULL;
863
864 spin_unlock_irqrestore(&hp->lock, flags);
865}
866
867/* called with hp->lock held */
868static void hvsi_push(struct hvsi_struct *hp)
869{
870 int n;
871
872 if (hp->n_outbuf <= 0)
873 return;
874
875 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
876 if (n > 0) {
877 /* success */
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700878 pr_debug("%s: wrote %i chars\n", __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 hp->n_outbuf = 0;
880 } else if (n == -EIO) {
881 __set_state(hp, HVSI_FSP_DIED);
882 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
883 }
884}
885
886/* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
David Howellsc4028952006-11-22 14:57:56 +0000887static void hvsi_write_worker(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
David Howellsc4028952006-11-22 14:57:56 +0000889 struct hvsi_struct *hp =
890 container_of(work, struct hvsi_struct, writer.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 unsigned long flags;
892#ifdef DEBUG
893 static long start_j = 0;
894
895 if (start_j == 0)
896 start_j = jiffies;
897#endif /* DEBUG */
898
899 spin_lock_irqsave(&hp->lock, flags);
900
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700901 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (!is_open(hp)) {
904 /*
905 * We could have a non-open connection if the service processor died
906 * while we were busily scheduling ourselves. In that case, it could
907 * be minutes before the service processor comes back, so only try
908 * again once a second.
909 */
910 schedule_delayed_work(&hp->writer, HZ);
911 goto out;
912 }
913
914 hvsi_push(hp);
915 if (hp->n_outbuf > 0)
916 schedule_delayed_work(&hp->writer, 10);
917 else {
918#ifdef DEBUG
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700919 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 jiffies - start_j);
921 start_j = 0;
922#endif /* DEBUG */
923 wake_up_all(&hp->emptyq);
Linas Vepstas64e0cc32006-08-05 12:14:39 -0700924 tty_wakeup(hp->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
927out:
928 spin_unlock_irqrestore(&hp->lock, flags);
929}
930
931static int hvsi_write_room(struct tty_struct *tty)
932{
Alan Coxc9f19e92009-01-02 13:47:26 +0000933 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 return N_OUTBUF - hp->n_outbuf;
936}
937
938static int hvsi_chars_in_buffer(struct tty_struct *tty)
939{
Alan Coxc9f19e92009-01-02 13:47:26 +0000940 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 return hp->n_outbuf;
943}
944
945static int hvsi_write(struct tty_struct *tty,
946 const unsigned char *buf, int count)
947{
948 struct hvsi_struct *hp = tty->driver_data;
949 const char *source = buf;
950 unsigned long flags;
951 int total = 0;
952 int origcount = count;
953
954 spin_lock_irqsave(&hp->lock, flags);
955
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700956 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (!is_open(hp)) {
959 /* we're either closing or not yet open; don't accept data */
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700960 pr_debug("%s: not open\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 goto out;
962 }
963
964 /*
965 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
966 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
967 * will see there is no room in outbuf and return.
968 */
969 while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) {
970 int chunksize = min(count, hvsi_write_room(hp->tty));
971
972 BUG_ON(hp->n_outbuf < 0);
973 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
974 hp->n_outbuf += chunksize;
975
976 total += chunksize;
977 source += chunksize;
978 count -= chunksize;
979 hvsi_push(hp);
980 }
981
982 if (hp->n_outbuf > 0) {
983 /*
984 * we weren't able to write it all to the hypervisor.
985 * schedule another push attempt.
986 */
987 schedule_delayed_work(&hp->writer, 10);
988 }
989
990out:
991 spin_unlock_irqrestore(&hp->lock, flags);
992
993 if (total != origcount)
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700994 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 total);
996
997 return total;
998}
999
1000/*
1001 * I have never seen throttle or unthrottle called, so this little throttle
1002 * buffering scheme may or may not work.
1003 */
1004static void hvsi_throttle(struct tty_struct *tty)
1005{
Alan Coxc9f19e92009-01-02 13:47:26 +00001006 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001008 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
1011}
1012
1013static void hvsi_unthrottle(struct tty_struct *tty)
1014{
Alan Coxc9f19e92009-01-02 13:47:26 +00001015 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 unsigned long flags;
1017 int shouldflip = 0;
1018
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001019 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 spin_lock_irqsave(&hp->lock, flags);
1022 if (hp->n_throttle) {
1023 hvsi_send_overflow(hp);
1024 shouldflip = 1;
1025 }
1026 spin_unlock_irqrestore(&hp->lock, flags);
1027
1028 if (shouldflip)
1029 tty_flip_buffer_push(hp->tty);
1030
1031 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1032}
1033
Alan Cox60b33c12011-02-14 16:26:14 +00001034static int hvsi_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Alan Coxc9f19e92009-01-02 13:47:26 +00001036 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 hvsi_get_mctrl(hp);
1039 return hp->mctrl;
1040}
1041
Alan Cox20b9d172011-02-14 16:26:50 +00001042static int hvsi_tiocmset(struct tty_struct *tty,
1043 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Alan Coxc9f19e92009-01-02 13:47:26 +00001045 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 unsigned long flags;
1047 uint16_t new_mctrl;
1048
1049 /* we can only alter DTR */
1050 clear &= TIOCM_DTR;
1051 set &= TIOCM_DTR;
1052
1053 spin_lock_irqsave(&hp->lock, flags);
1054
1055 new_mctrl = (hp->mctrl & ~clear) | set;
1056
1057 if (hp->mctrl != new_mctrl) {
1058 hvsi_set_mctrl(hp, new_mctrl);
1059 hp->mctrl = new_mctrl;
1060 }
1061 spin_unlock_irqrestore(&hp->lock, flags);
1062
1063 return 0;
1064}
1065
1066
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001067static const struct tty_operations hvsi_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 .open = hvsi_open,
1069 .close = hvsi_close,
1070 .write = hvsi_write,
1071 .hangup = hvsi_hangup,
1072 .write_room = hvsi_write_room,
1073 .chars_in_buffer = hvsi_chars_in_buffer,
1074 .throttle = hvsi_throttle,
1075 .unthrottle = hvsi_unthrottle,
1076 .tiocmget = hvsi_tiocmget,
1077 .tiocmset = hvsi_tiocmset,
1078};
1079
1080static int __init hvsi_init(void)
1081{
1082 int i;
1083
1084 hvsi_driver = alloc_tty_driver(hvsi_count);
1085 if (!hvsi_driver)
1086 return -ENOMEM;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 hvsi_driver->driver_name = "hvsi";
1089 hvsi_driver->name = "hvsi";
1090 hvsi_driver->major = HVSI_MAJOR;
1091 hvsi_driver->minor_start = HVSI_MINOR;
1092 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1093 hvsi_driver->init_termios = tty_std_termios;
1094 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001095 hvsi_driver->init_termios.c_ispeed = 9600;
1096 hvsi_driver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1098 tty_set_operations(hvsi_driver, &hvsi_ops);
1099
1100 for (i=0; i < hvsi_count; i++) {
1101 struct hvsi_struct *hp = &hvsi_ports[i];
1102 int ret = 1;
1103
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001104 ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (ret)
1106 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1107 hp->virq, ret);
1108 }
1109 hvsi_wait = wait_for_state; /* irqs active now */
1110
1111 if (tty_register_driver(hvsi_driver))
1112 panic("Couldn't register hvsi console driver\n");
1113
Olof Johansson8b6a7b22006-04-12 15:19:50 -05001114 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 return 0;
1117}
1118device_initcall(hvsi_init);
1119
1120/***** console (not tty) code: *****/
1121
1122static void hvsi_console_print(struct console *console, const char *buf,
1123 unsigned int count)
1124{
1125 struct hvsi_struct *hp = &hvsi_ports[console->index];
1126 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1127 unsigned int i = 0, n = 0;
1128 int ret, donecr = 0;
1129
1130 mb();
1131 if (!is_open(hp))
1132 return;
1133
1134 /*
1135 * ugh, we have to translate LF -> CRLF ourselves, in place.
1136 * copied from hvc_console.c:
1137 */
1138 while (count > 0 || i > 0) {
1139 if (count > 0 && i < sizeof(c)) {
1140 if (buf[n] == '\n' && !donecr) {
1141 c[i++] = '\r';
1142 donecr = 1;
1143 } else {
1144 c[i++] = buf[n++];
1145 donecr = 0;
1146 --count;
1147 }
1148 } else {
1149 ret = hvsi_put_chars(hp, c, i);
1150 if (ret < 0)
1151 i = 0;
1152 i -= ret;
1153 }
1154 }
1155}
1156
1157static struct tty_driver *hvsi_console_device(struct console *console,
1158 int *index)
1159{
1160 *index = console->index;
1161 return hvsi_driver;
1162}
1163
1164static int __init hvsi_console_setup(struct console *console, char *options)
1165{
Roel Kluin72865652009-08-06 13:00:37 +00001166 struct hvsi_struct *hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int ret;
1168
1169 if (console->index < 0 || console->index >= hvsi_count)
1170 return -1;
Roel Kluin72865652009-08-06 13:00:37 +00001171 hp = &hvsi_ports[console->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /* give the FSP a chance to change the baud rate when we re-open */
1174 hvsi_close_protocol(hp);
1175
1176 ret = hvsi_handshake(hp);
1177 if (ret < 0)
1178 return ret;
1179
1180 ret = hvsi_get_mctrl(hp);
1181 if (ret < 0)
1182 return ret;
1183
1184 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1185 if (ret < 0)
1186 return ret;
1187
1188 hp->flags |= HVSI_CONSOLE;
1189
1190 return 0;
1191}
1192
Chris Metcalf4455b112010-07-06 08:03:16 +00001193static struct console hvsi_console = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 .name = "hvsi",
1195 .write = hvsi_console_print,
1196 .device = hvsi_console_device,
1197 .setup = hvsi_console_setup,
1198 .flags = CON_PRINTBUFFER,
1199 .index = -1,
1200};
1201
1202static int __init hvsi_console_init(void)
1203{
1204 struct device_node *vty;
1205
1206 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1207
1208 /* search device tree for vty nodes */
1209 for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol");
1210 vty != NULL;
1211 vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) {
1212 struct hvsi_struct *hp;
Jeremy Kerr954a46e2006-07-12 15:39:43 +10001213 const uint32_t *vtermno, *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Stephen Rothwell01b27262007-04-27 13:41:15 +10001215 vtermno = of_get_property(vty, "reg", NULL);
1216 irq = of_get_property(vty, "interrupts", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!vtermno || !irq)
1218 continue;
1219
1220 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1221 of_node_put(vty);
1222 break;
1223 }
1224
1225 hp = &hvsi_ports[hvsi_count];
David Howellsc4028952006-11-22 14:57:56 +00001226 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1227 INIT_WORK(&hp->handshaker, hvsi_handshaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 init_waitqueue_head(&hp->emptyq);
1229 init_waitqueue_head(&hp->stateq);
1230 spin_lock_init(&hp->lock);
1231 hp->index = hvsi_count;
1232 hp->inbuf_end = hp->inbuf;
1233 hp->state = HVSI_CLOSED;
1234 hp->vtermno = *vtermno;
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -07001235 hp->virq = irq_create_mapping(NULL, irq[0]);
Alan Coxd4e33fa2012-01-26 17:44:09 +00001236 if (hp->virq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001238 __func__, irq[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 continue;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 hvsi_count++;
1243 }
1244
1245 if (hvsi_count)
Chris Metcalf4455b112010-07-06 08:03:16 +00001246 register_console(&hvsi_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return 0;
1248}
1249console_initcall(hvsi_console_init);