blob: ef95a154854abc9622999df1f5248e683e3d85d5 [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 {
Jiri Slabyd73a4e72012-04-02 13:54:28 +020072 struct tty_port port;
David Howellsc4028952006-11-22 14:57:56 +000073 struct delayed_work writer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct work_struct handshaker;
75 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
76 wait_queue_head_t stateq; /* woken when HVSI state changes */
77 spinlock_t lock;
78 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline int is_console(struct hvsi_struct *hp)
113{
114 return hp->flags & HVSI_CONSOLE;
115}
116
117static inline int is_open(struct hvsi_struct *hp)
118{
119 /* if we're waiting for an mctrl then we're already open */
120 return (hp->state == HVSI_OPEN)
121 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
122}
123
124static inline void print_state(struct hvsi_struct *hp)
125{
126#ifdef DEBUG
127 static const char *state_names[] = {
128 "HVSI_CLOSED",
129 "HVSI_WAIT_FOR_VER_RESPONSE",
130 "HVSI_WAIT_FOR_VER_QUERY",
131 "HVSI_OPEN",
132 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
133 "HVSI_FSP_DIED",
134 };
Phil Carmody08a82c62010-05-24 14:33:04 -0700135 const char *name = (hp->state < ARRAY_SIZE(state_names))
136 ? state_names[hp->state] : "UNKNOWN";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 pr_debug("hvsi%i: state = %s\n", hp->index, name);
139#endif /* DEBUG */
140}
141
142static inline void __set_state(struct hvsi_struct *hp, int state)
143{
144 hp->state = state;
145 print_state(hp);
146 wake_up_all(&hp->stateq);
147}
148
149static inline void set_state(struct hvsi_struct *hp, int state)
150{
151 unsigned long flags;
152
153 spin_lock_irqsave(&hp->lock, flags);
154 __set_state(hp, state);
155 spin_unlock_irqrestore(&hp->lock, flags);
156}
157
158static inline int len_packet(const uint8_t *packet)
159{
160 return (int)((struct hvsi_header *)packet)->len;
161}
162
163static inline int is_header(const uint8_t *packet)
164{
165 struct hvsi_header *header = (struct hvsi_header *)packet;
166 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
167}
168
169static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
170{
171 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
172 return 0; /* don't even have the packet header */
173
174 if (hp->inbuf_end < (packet + len_packet(packet)))
175 return 0; /* don't have the rest of the packet */
176
177 return 1;
178}
179
180/* shift remaining bytes in packetbuf down */
181static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
182{
183 int remaining = (int)(hp->inbuf_end - read_to);
184
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700185 pr_debug("%s: %i chars remain\n", __func__, remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (read_to != hp->inbuf)
188 memmove(hp->inbuf, read_to, remaining);
189
190 hp->inbuf_end = hp->inbuf + remaining;
191}
192
193#ifdef DEBUG
194#define dbg_dump_packet(packet) dump_packet(packet)
195#define dbg_dump_hex(data, len) dump_hex(data, len)
196#else
197#define dbg_dump_packet(packet) do { } while (0)
198#define dbg_dump_hex(data, len) do { } while (0)
199#endif
200
201static void dump_hex(const uint8_t *data, int len)
202{
203 int i;
204
205 printk(" ");
206 for (i=0; i < len; i++)
207 printk("%.2x", data[i]);
208
209 printk("\n ");
210 for (i=0; i < len; i++) {
211 if (isprint(data[i]))
212 printk("%c", data[i]);
213 else
214 printk(".");
215 }
216 printk("\n");
217}
218
219static void dump_packet(uint8_t *packet)
220{
221 struct hvsi_header *header = (struct hvsi_header *)packet;
222
223 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
224 header->seqno);
225
226 dump_hex(packet, header->len);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
230{
231 unsigned long got;
232
Milton Miller88de0be2005-07-07 17:56:27 -0700233 got = hvc_get_chars(hp->vtermno, buf, count);
234
235 return got;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
Jiri Slaby28c04472012-04-02 13:54:29 +0200239 struct tty_struct *tty, struct hvsi_struct **to_handshake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 struct hvsi_control *header = (struct hvsi_control *)packet;
242
243 switch (header->verb) {
244 case VSV_MODEM_CTL_UPDATE:
245 if ((header->word & HVSI_TSCD) == 0) {
246 /* CD went away; no more connection */
247 pr_debug("hvsi%i: CD dropped\n", hp->index);
248 hp->mctrl &= TIOCM_CD;
Jiri Slaby28c04472012-04-02 13:54:29 +0200249 if (tty && !C_CLOCAL(tty))
250 tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 break;
253 case VSV_CLOSE_PROTOCOL:
254 pr_debug("hvsi%i: service processor came back\n", hp->index);
255 if (hp->state != HVSI_CLOSED) {
256 *to_handshake = hp;
257 }
258 break;
259 default:
260 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
261 hp->index);
262 dump_packet(packet);
263 break;
264 }
265}
266
267static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
268{
269 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
270
271 switch (hp->state) {
272 case HVSI_WAIT_FOR_VER_RESPONSE:
273 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
274 break;
275 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
276 hp->mctrl = 0;
277 if (resp->u.mctrl_word & HVSI_TSDTR)
278 hp->mctrl |= TIOCM_DTR;
279 if (resp->u.mctrl_word & HVSI_TSCD)
280 hp->mctrl |= TIOCM_CD;
281 __set_state(hp, HVSI_OPEN);
282 break;
283 default:
284 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
285 dump_packet(packet);
286 break;
287 }
288}
289
290/* respond to service processor's version query */
291static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
292{
293 struct hvsi_query_response packet __ALIGNED__;
294 int wrote;
295
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000296 packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
297 packet.hdr.len = sizeof(struct hvsi_query_response);
298 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 packet.verb = VSV_SEND_VERSION_NUMBER;
300 packet.u.version = HVSI_VERSION;
301 packet.query_seqno = query_seqno+1;
302
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000303 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
304 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000306 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
307 if (wrote != packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
309 hp->index);
310 return -EIO;
311 }
312
313 return 0;
314}
315
316static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
317{
318 struct hvsi_query *query = (struct hvsi_query *)packet;
319
320 switch (hp->state) {
321 case HVSI_WAIT_FOR_VER_QUERY:
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000322 hvsi_version_respond(hp, query->hdr.seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 __set_state(hp, HVSI_OPEN);
324 break;
325 default:
326 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
327 dump_packet(packet);
328 break;
329 }
330}
331
Jiri Slaby92a19f92013-01-03 15:53:03 +0100332static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 int i;
335
336 for (i=0; i < len; i++) {
337 char c = buf[i];
338#ifdef CONFIG_MAGIC_SYSRQ
339 if (c == '\0') {
340 hp->sysrq = 1;
341 continue;
342 } else if (hp->sysrq) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700343 handle_sysrq(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 hp->sysrq = 0;
345 continue;
346 }
347#endif /* CONFIG_MAGIC_SYSRQ */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100348 tty_insert_flip_char(&hp->port, c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350}
351
352/*
353 * We could get 252 bytes of data at once here. But the tty layer only
354 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
355 * it. Accordingly we won't send more than 128 bytes at a time to the flip
356 * buffer, which will give the tty buffer a chance to throttle us. Should the
357 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
358 * revisited.
359 */
360#define TTY_THRESHOLD_THROTTLE 128
Jiri Slaby92a19f92013-01-03 15:53:03 +0100361static bool hvsi_recv_data(struct hvsi_struct *hp, const uint8_t *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 const struct hvsi_header *header = (const struct hvsi_header *)packet;
364 const uint8_t *data = packet + sizeof(struct hvsi_header);
365 int datalen = header->len - sizeof(struct hvsi_header);
366 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
367
368 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
369
370 if (datalen == 0)
Jiri Slaby28c04472012-04-02 13:54:29 +0200371 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (overflow > 0) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700374 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 datalen = TTY_THRESHOLD_THROTTLE;
376 }
377
Jiri Slaby92a19f92013-01-03 15:53:03 +0100378 hvsi_insert_chars(hp, data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (overflow > 0) {
381 /*
382 * we still have more data to deliver, so we need to save off the
383 * overflow and send it later
384 */
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700385 pr_debug("%s: deferring overflow\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
387 hp->n_throttle = overflow;
388 }
389
Jiri Slaby28c04472012-04-02 13:54:29 +0200390 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/*
394 * Returns true/false indicating data successfully read from hypervisor.
395 * Used both to get packets for tty connections and to advance the state
396 * machine during console handshaking (in which case tty = NULL and we ignore
397 * incoming data).
398 */
Jiri Slaby28c04472012-04-02 13:54:29 +0200399static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty,
400 struct hvsi_struct **handshake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 uint8_t *packet = hp->inbuf;
403 int chunklen;
Jiri Slaby28c04472012-04-02 13:54:29 +0200404 bool flip = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *handshake = NULL;
407
408 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
409 if (chunklen == 0) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700410 pr_debug("%s: 0-length read\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
412 }
413
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700414 pr_debug("%s: got %i bytes\n", __func__, chunklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 dbg_dump_hex(hp->inbuf_end, chunklen);
416
417 hp->inbuf_end += chunklen;
418
419 /* handle all completed packets */
420 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
421 struct hvsi_header *header = (struct hvsi_header *)packet;
422
423 if (!is_header(packet)) {
424 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
425 /* skip bytes until we find a header or run out of data */
426 while ((packet < hp->inbuf_end) && (!is_header(packet)))
427 packet++;
428 continue;
429 }
430
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700431 pr_debug("%s: handling %i-byte packet\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 len_packet(packet));
433 dbg_dump_packet(packet);
434
435 switch (header->type) {
436 case VS_DATA_PACKET_HEADER:
437 if (!is_open(hp))
438 break;
Jiri Slaby92a19f92013-01-03 15:53:03 +0100439 flip = hvsi_recv_data(hp, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441 case VS_CONTROL_PACKET_HEADER:
Jiri Slaby28c04472012-04-02 13:54:29 +0200442 hvsi_recv_control(hp, packet, tty, handshake);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 case VS_QUERY_RESPONSE_PACKET_HEADER:
445 hvsi_recv_response(hp, packet);
446 break;
447 case VS_QUERY_PACKET_HEADER:
448 hvsi_recv_query(hp, packet);
449 break;
450 default:
451 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
452 hp->index, header->type);
453 dump_packet(packet);
454 break;
455 }
456
457 packet += len_packet(packet);
458
Jiri Slaby28c04472012-04-02 13:54:29 +0200459 if (*handshake) {
460 pr_debug("%s: handshake\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 }
463 }
464
465 compact_inbuf(hp, packet);
466
Jiri Slaby28c04472012-04-02 13:54:29 +0200467 if (flip)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100468 tty_flip_buffer_push(&hp->port);
Jiri Slaby28c04472012-04-02 13:54:29 +0200469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 1;
471}
472
Jiri Slaby92a19f92013-01-03 15:53:03 +0100473static void hvsi_send_overflow(struct hvsi_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700475 pr_debug("%s: delivering %i bytes overflow\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 hp->n_throttle);
477
Jiri Slaby92a19f92013-01-03 15:53:03 +0100478 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 hp->n_throttle = 0;
480}
481
482/*
483 * must get all pending data because we only get an irq on empty->non-empty
484 * transition
485 */
David Howells7d12e782006-10-05 14:55:46 +0100486static irqreturn_t hvsi_interrupt(int irq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct hvsi_struct *handshake;
Jiri Slabydaea4402012-04-02 13:54:30 +0200490 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 unsigned long flags;
492 int again = 1;
493
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700494 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Jiri Slabydaea4402012-04-02 13:54:30 +0200496 tty = tty_port_tty_get(&hp->port);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 while (again) {
499 spin_lock_irqsave(&hp->lock, flags);
Jiri Slabydaea4402012-04-02 13:54:30 +0200500 again = hvsi_load_chunk(hp, tty, &handshake);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 spin_unlock_irqrestore(&hp->lock, flags);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (handshake) {
504 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
505 schedule_work(&handshake->handshaker);
506 }
507 }
508
509 spin_lock_irqsave(&hp->lock, flags);
Jiri Slabydaea4402012-04-02 13:54:30 +0200510 if (tty && hp->n_throttle && !test_bit(TTY_THROTTLED, &tty->flags)) {
511 /* we weren't hung up and we weren't throttled, so we can
512 * deliver the rest now */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100513 hvsi_send_overflow(hp);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100514 tty_flip_buffer_push(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 spin_unlock_irqrestore(&hp->lock, flags);
517
Jiri Slabydaea4402012-04-02 13:54:30 +0200518 tty_kref_put(tty);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return IRQ_HANDLED;
521}
522
523/* for boot console, before the irq handler is running */
524static int __init poll_for_state(struct hvsi_struct *hp, int state)
525{
526 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
527
528 for (;;) {
David Howells7d12e782006-10-05 14:55:46 +0100529 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (hp->state == state)
532 return 0;
533
534 mdelay(5);
535 if (time_after(jiffies, end_jiffies))
536 return -EIO;
537 }
538}
539
540/* wait for irq handler to change our state */
541static int wait_for_state(struct hvsi_struct *hp, int state)
542{
543 int ret = 0;
544
545 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
546 ret = -EIO;
547
548 return ret;
549}
550
551static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
552{
553 struct hvsi_query packet __ALIGNED__;
554 int wrote;
555
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000556 packet.hdr.type = VS_QUERY_PACKET_HEADER;
557 packet.hdr.len = sizeof(struct hvsi_query);
558 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 packet.verb = verb;
560
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000561 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
562 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000564 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
565 if (wrote != packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
567 wrote);
568 return -EIO;
569 }
570
571 return 0;
572}
573
574static int hvsi_get_mctrl(struct hvsi_struct *hp)
575{
576 int ret;
577
578 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
579 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
580
581 ret = hvsi_wait(hp, HVSI_OPEN);
582 if (ret < 0) {
583 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
584 set_state(hp, HVSI_OPEN);
585 return ret;
586 }
587
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700588 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 return 0;
591}
592
593/* note that we can only set DTR */
594static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
595{
596 struct hvsi_control packet __ALIGNED__;
597 int wrote;
598
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000599 packet.hdr.type = VS_CONTROL_PACKET_HEADER,
600 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
601 packet.hdr.len = sizeof(struct hvsi_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 packet.verb = VSV_SET_MODEM_CTL;
603 packet.mask = HVSI_TSDTR;
604
605 if (mctrl & TIOCM_DTR)
606 packet.word = HVSI_TSDTR;
607
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000608 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
609 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000611 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
612 if (wrote != packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
614 return -EIO;
615 }
616
617 return 0;
618}
619
620static void hvsi_drain_input(struct hvsi_struct *hp)
621{
622 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
623 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
624
625 while (time_before(end_jiffies, jiffies))
626 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
627 break;
628}
629
630static int hvsi_handshake(struct hvsi_struct *hp)
631{
632 int ret;
633
634 /*
635 * We could have a CLOSE or other data waiting for us before we even try
636 * to open; try to throw it all away so we don't get confused. (CLOSE
637 * is the first message sent up the pipe when the FSP comes online. We
638 * need to distinguish between "it came up a while ago and we're the first
639 * user" and "it was just reset before it saw our handshake packet".)
640 */
641 hvsi_drain_input(hp);
642
643 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
644 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
645 if (ret < 0) {
646 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
647 return ret;
648 }
649
650 ret = hvsi_wait(hp, HVSI_OPEN);
651 if (ret < 0)
652 return ret;
653
654 return 0;
655}
656
David Howellsc4028952006-11-22 14:57:56 +0000657static void hvsi_handshaker(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
David Howellsc4028952006-11-22 14:57:56 +0000659 struct hvsi_struct *hp =
660 container_of(work, struct hvsi_struct, handshaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (hvsi_handshake(hp) >= 0)
663 return;
664
665 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
666 if (is_console(hp)) {
667 /*
668 * ttys will re-attempt the handshake via hvsi_open, but
669 * the console will not.
670 */
671 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
672 }
673}
674
675static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
676{
677 struct hvsi_data packet __ALIGNED__;
678 int ret;
679
680 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
681
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000682 packet.hdr.type = VS_DATA_PACKET_HEADER;
683 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
684 packet.hdr.len = count + sizeof(struct hvsi_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 memcpy(&packet.data, buf, count);
686
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000687 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
688 if (ret == packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* return the number of chars written, not the packet length */
690 return count;
691 }
692 return ret; /* return any errors */
693}
694
695static void hvsi_close_protocol(struct hvsi_struct *hp)
696{
697 struct hvsi_control packet __ALIGNED__;
698
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000699 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
700 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
701 packet.hdr.len = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 packet.verb = VSV_CLOSE_PROTOCOL;
703
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000704 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
705 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000707 hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static int hvsi_open(struct tty_struct *tty, struct file *filp)
711{
712 struct hvsi_struct *hp;
713 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int ret;
715
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700716 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jiri Slaby410235f2012-03-05 14:52:01 +0100718 hp = &hvsi_ports[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 tty->driver_data = hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 mb();
723 if (hp->state == HVSI_FSP_DIED)
724 return -EIO;
725
Jiri Slabydaea4402012-04-02 13:54:30 +0200726 tty_port_tty_set(&hp->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 spin_lock_irqsave(&hp->lock, flags);
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200728 hp->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 atomic_set(&hp->seqno, 0);
730 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
731 spin_unlock_irqrestore(&hp->lock, flags);
732
733 if (is_console(hp))
734 return 0; /* this has already been handshaked as the console */
735
736 ret = hvsi_handshake(hp);
737 if (ret < 0) {
738 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
739 return ret;
740 }
741
742 ret = hvsi_get_mctrl(hp);
743 if (ret < 0) {
744 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
745 return ret;
746 }
747
748 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
749 if (ret < 0) {
750 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
751 return ret;
752 }
753
754 return 0;
755}
756
757/* wait for hvsi_write_worker to empty hp->outbuf */
758static void hvsi_flush_output(struct hvsi_struct *hp)
759{
760 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
761
762 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
Tejun Heo42565992010-12-24 15:59:07 +0100763 cancel_delayed_work_sync(&hp->writer);
Tejun Heo43829732012-08-20 14:51:24 -0700764 flush_work(&hp->handshaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /*
767 * it's also possible that our timeout expired and hvsi_write_worker
768 * didn't manage to push outbuf. poof.
769 */
770 hp->n_outbuf = 0;
771}
772
773static void hvsi_close(struct tty_struct *tty, struct file *filp)
774{
775 struct hvsi_struct *hp = tty->driver_data;
776 unsigned long flags;
777
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700778 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (tty_hung_up_p(filp))
781 return;
782
783 spin_lock_irqsave(&hp->lock, flags);
784
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200785 if (--hp->port.count == 0) {
Jiri Slabydaea4402012-04-02 13:54:30 +0200786 tty_port_tty_set(&hp->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
788
789 /* only close down connection if it is not the console */
790 if (!is_console(hp)) {
791 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
792 __set_state(hp, HVSI_CLOSED);
793 /*
794 * any data delivered to the tty layer after this will be
795 * discarded (except for XON/XOFF)
796 */
797 tty->closing = 1;
798
799 spin_unlock_irqrestore(&hp->lock, flags);
800
801 /* let any existing irq handlers finish. no more will start. */
802 synchronize_irq(hp->virq);
803
804 /* hvsi_write_worker will re-schedule until outbuf is empty. */
805 hvsi_flush_output(hp);
806
807 /* tell FSP to stop sending data */
808 hvsi_close_protocol(hp);
809
810 /*
811 * drain anything FSP is still in the middle of sending, and let
812 * hvsi_handshake drain the rest on the next open.
813 */
814 hvsi_drain_input(hp);
815
816 spin_lock_irqsave(&hp->lock, flags);
817 }
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200818 } else if (hp->port.count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200820 hp - hvsi_ports, hp->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 spin_unlock_irqrestore(&hp->lock, flags);
823}
824
825static void hvsi_hangup(struct tty_struct *tty)
826{
827 struct hvsi_struct *hp = tty->driver_data;
828 unsigned long flags;
829
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700830 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Jiri Slabydaea4402012-04-02 13:54:30 +0200832 tty_port_tty_set(&hp->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Jiri Slabydaea4402012-04-02 13:54:30 +0200834 spin_lock_irqsave(&hp->lock, flags);
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200835 hp->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 hp->n_outbuf = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 spin_unlock_irqrestore(&hp->lock, flags);
838}
839
840/* called with hp->lock held */
841static void hvsi_push(struct hvsi_struct *hp)
842{
843 int n;
844
845 if (hp->n_outbuf <= 0)
846 return;
847
848 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
849 if (n > 0) {
850 /* success */
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700851 pr_debug("%s: wrote %i chars\n", __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 hp->n_outbuf = 0;
853 } else if (n == -EIO) {
854 __set_state(hp, HVSI_FSP_DIED);
855 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
856 }
857}
858
859/* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
David Howellsc4028952006-11-22 14:57:56 +0000860static void hvsi_write_worker(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
David Howellsc4028952006-11-22 14:57:56 +0000862 struct hvsi_struct *hp =
863 container_of(work, struct hvsi_struct, writer.work);
Jiri Slabydaea4402012-04-02 13:54:30 +0200864 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 unsigned long flags;
866#ifdef DEBUG
867 static long start_j = 0;
868
869 if (start_j == 0)
870 start_j = jiffies;
871#endif /* DEBUG */
872
873 spin_lock_irqsave(&hp->lock, flags);
874
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700875 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (!is_open(hp)) {
878 /*
879 * We could have a non-open connection if the service processor died
880 * while we were busily scheduling ourselves. In that case, it could
881 * be minutes before the service processor comes back, so only try
882 * again once a second.
883 */
884 schedule_delayed_work(&hp->writer, HZ);
885 goto out;
886 }
887
888 hvsi_push(hp);
889 if (hp->n_outbuf > 0)
890 schedule_delayed_work(&hp->writer, 10);
891 else {
892#ifdef DEBUG
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700893 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 jiffies - start_j);
895 start_j = 0;
896#endif /* DEBUG */
897 wake_up_all(&hp->emptyq);
Jiri Slabydaea4402012-04-02 13:54:30 +0200898 tty = tty_port_tty_get(&hp->port);
899 if (tty) {
900 tty_wakeup(tty);
901 tty_kref_put(tty);
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905out:
906 spin_unlock_irqrestore(&hp->lock, flags);
907}
908
909static int hvsi_write_room(struct tty_struct *tty)
910{
Alan Coxc9f19e92009-01-02 13:47:26 +0000911 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 return N_OUTBUF - hp->n_outbuf;
914}
915
916static int hvsi_chars_in_buffer(struct tty_struct *tty)
917{
Alan Coxc9f19e92009-01-02 13:47:26 +0000918 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 return hp->n_outbuf;
921}
922
923static int hvsi_write(struct tty_struct *tty,
924 const unsigned char *buf, int count)
925{
926 struct hvsi_struct *hp = tty->driver_data;
927 const char *source = buf;
928 unsigned long flags;
929 int total = 0;
930 int origcount = count;
931
932 spin_lock_irqsave(&hp->lock, flags);
933
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700934 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (!is_open(hp)) {
937 /* we're either closing or not yet open; don't accept data */
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700938 pr_debug("%s: not open\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 goto out;
940 }
941
942 /*
943 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
944 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
945 * will see there is no room in outbuf and return.
946 */
Jiri Slaby28c04472012-04-02 13:54:29 +0200947 while ((count > 0) && (hvsi_write_room(tty) > 0)) {
948 int chunksize = min(count, hvsi_write_room(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 BUG_ON(hp->n_outbuf < 0);
951 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
952 hp->n_outbuf += chunksize;
953
954 total += chunksize;
955 source += chunksize;
956 count -= chunksize;
957 hvsi_push(hp);
958 }
959
960 if (hp->n_outbuf > 0) {
961 /*
962 * we weren't able to write it all to the hypervisor.
963 * schedule another push attempt.
964 */
965 schedule_delayed_work(&hp->writer, 10);
966 }
967
968out:
969 spin_unlock_irqrestore(&hp->lock, flags);
970
971 if (total != origcount)
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700972 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 total);
974
975 return total;
976}
977
978/*
979 * I have never seen throttle or unthrottle called, so this little throttle
980 * buffering scheme may or may not work.
981 */
982static void hvsi_throttle(struct tty_struct *tty)
983{
Alan Coxc9f19e92009-01-02 13:47:26 +0000984 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700986 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
989}
990
991static void hvsi_unthrottle(struct tty_struct *tty)
992{
Alan Coxc9f19e92009-01-02 13:47:26 +0000993 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700996 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 spin_lock_irqsave(&hp->lock, flags);
999 if (hp->n_throttle) {
Jiri Slaby92a19f92013-01-03 15:53:03 +01001000 hvsi_send_overflow(hp);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001001 tty_flip_buffer_push(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 spin_unlock_irqrestore(&hp->lock, flags);
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1007}
1008
Alan Cox60b33c12011-02-14 16:26:14 +00001009static int hvsi_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Alan Coxc9f19e92009-01-02 13:47:26 +00001011 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 hvsi_get_mctrl(hp);
1014 return hp->mctrl;
1015}
1016
Alan Cox20b9d172011-02-14 16:26:50 +00001017static int hvsi_tiocmset(struct tty_struct *tty,
1018 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Alan Coxc9f19e92009-01-02 13:47:26 +00001020 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 unsigned long flags;
1022 uint16_t new_mctrl;
1023
1024 /* we can only alter DTR */
1025 clear &= TIOCM_DTR;
1026 set &= TIOCM_DTR;
1027
1028 spin_lock_irqsave(&hp->lock, flags);
1029
1030 new_mctrl = (hp->mctrl & ~clear) | set;
1031
1032 if (hp->mctrl != new_mctrl) {
1033 hvsi_set_mctrl(hp, new_mctrl);
1034 hp->mctrl = new_mctrl;
1035 }
1036 spin_unlock_irqrestore(&hp->lock, flags);
1037
1038 return 0;
1039}
1040
1041
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001042static const struct tty_operations hvsi_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .open = hvsi_open,
1044 .close = hvsi_close,
1045 .write = hvsi_write,
1046 .hangup = hvsi_hangup,
1047 .write_room = hvsi_write_room,
1048 .chars_in_buffer = hvsi_chars_in_buffer,
1049 .throttle = hvsi_throttle,
1050 .unthrottle = hvsi_unthrottle,
1051 .tiocmget = hvsi_tiocmget,
1052 .tiocmset = hvsi_tiocmset,
1053};
1054
1055static int __init hvsi_init(void)
1056{
1057 int i;
1058
1059 hvsi_driver = alloc_tty_driver(hvsi_count);
1060 if (!hvsi_driver)
1061 return -ENOMEM;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 hvsi_driver->driver_name = "hvsi";
1064 hvsi_driver->name = "hvsi";
1065 hvsi_driver->major = HVSI_MAJOR;
1066 hvsi_driver->minor_start = HVSI_MINOR;
1067 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1068 hvsi_driver->init_termios = tty_std_termios;
1069 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001070 hvsi_driver->init_termios.c_ispeed = 9600;
1071 hvsi_driver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1073 tty_set_operations(hvsi_driver, &hvsi_ops);
1074
1075 for (i=0; i < hvsi_count; i++) {
1076 struct hvsi_struct *hp = &hvsi_ports[i];
1077 int ret = 1;
1078
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001079 tty_port_link_device(&hp->port, hvsi_driver, i);
1080
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001081 ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (ret)
1083 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1084 hp->virq, ret);
1085 }
1086 hvsi_wait = wait_for_state; /* irqs active now */
1087
1088 if (tty_register_driver(hvsi_driver))
1089 panic("Couldn't register hvsi console driver\n");
1090
Olof Johansson8b6a7b22006-04-12 15:19:50 -05001091 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 return 0;
1094}
1095device_initcall(hvsi_init);
1096
1097/***** console (not tty) code: *****/
1098
1099static void hvsi_console_print(struct console *console, const char *buf,
1100 unsigned int count)
1101{
1102 struct hvsi_struct *hp = &hvsi_ports[console->index];
1103 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1104 unsigned int i = 0, n = 0;
1105 int ret, donecr = 0;
1106
1107 mb();
1108 if (!is_open(hp))
1109 return;
1110
1111 /*
1112 * ugh, we have to translate LF -> CRLF ourselves, in place.
1113 * copied from hvc_console.c:
1114 */
1115 while (count > 0 || i > 0) {
1116 if (count > 0 && i < sizeof(c)) {
1117 if (buf[n] == '\n' && !donecr) {
1118 c[i++] = '\r';
1119 donecr = 1;
1120 } else {
1121 c[i++] = buf[n++];
1122 donecr = 0;
1123 --count;
1124 }
1125 } else {
1126 ret = hvsi_put_chars(hp, c, i);
1127 if (ret < 0)
1128 i = 0;
1129 i -= ret;
1130 }
1131 }
1132}
1133
1134static struct tty_driver *hvsi_console_device(struct console *console,
1135 int *index)
1136{
1137 *index = console->index;
1138 return hvsi_driver;
1139}
1140
1141static int __init hvsi_console_setup(struct console *console, char *options)
1142{
Roel Kluin72865652009-08-06 13:00:37 +00001143 struct hvsi_struct *hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int ret;
1145
1146 if (console->index < 0 || console->index >= hvsi_count)
1147 return -1;
Roel Kluin72865652009-08-06 13:00:37 +00001148 hp = &hvsi_ports[console->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /* give the FSP a chance to change the baud rate when we re-open */
1151 hvsi_close_protocol(hp);
1152
1153 ret = hvsi_handshake(hp);
1154 if (ret < 0)
1155 return ret;
1156
1157 ret = hvsi_get_mctrl(hp);
1158 if (ret < 0)
1159 return ret;
1160
1161 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1162 if (ret < 0)
1163 return ret;
1164
1165 hp->flags |= HVSI_CONSOLE;
1166
1167 return 0;
1168}
1169
Chris Metcalf4455b112010-07-06 08:03:16 +00001170static struct console hvsi_console = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 .name = "hvsi",
1172 .write = hvsi_console_print,
1173 .device = hvsi_console_device,
1174 .setup = hvsi_console_setup,
1175 .flags = CON_PRINTBUFFER,
1176 .index = -1,
1177};
1178
1179static int __init hvsi_console_init(void)
1180{
1181 struct device_node *vty;
1182
1183 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1184
1185 /* search device tree for vty nodes */
Wei Yongjun074d35c2012-12-04 00:16:18 -05001186 for_each_compatible_node(vty, "serial", "hvterm-protocol") {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct hvsi_struct *hp;
Jeremy Kerr954a46e2006-07-12 15:39:43 +10001188 const uint32_t *vtermno, *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Stephen Rothwell01b27262007-04-27 13:41:15 +10001190 vtermno = of_get_property(vty, "reg", NULL);
1191 irq = of_get_property(vty, "interrupts", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!vtermno || !irq)
1193 continue;
1194
1195 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1196 of_node_put(vty);
1197 break;
1198 }
1199
1200 hp = &hvsi_ports[hvsi_count];
David Howellsc4028952006-11-22 14:57:56 +00001201 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1202 INIT_WORK(&hp->handshaker, hvsi_handshaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 init_waitqueue_head(&hp->emptyq);
1204 init_waitqueue_head(&hp->stateq);
1205 spin_lock_init(&hp->lock);
Jiri Slabyd73a4e72012-04-02 13:54:28 +02001206 tty_port_init(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 hp->index = hvsi_count;
1208 hp->inbuf_end = hp->inbuf;
1209 hp->state = HVSI_CLOSED;
1210 hp->vtermno = *vtermno;
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -07001211 hp->virq = irq_create_mapping(NULL, irq[0]);
Alan Coxd4e33fa2012-01-26 17:44:09 +00001212 if (hp->virq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001214 __func__, irq[0]);
Jiri Slaby191c5f12012-11-15 09:49:56 +01001215 tty_port_destroy(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 continue;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 hvsi_count++;
1220 }
1221
1222 if (hvsi_count)
Chris Metcalf4455b112010-07-06 08:03:16 +00001223 register_console(&hvsi_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return 0;
1225}
1226console_initcall(hvsi_console_init);