blob: 66f95f758be05722d326d34875f994e44431a16c [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6/* Host Virtual Serial Interface (HVSI) is a protocol between the hosted OS
7 * and the service processor on IBM pSeries servers. On these servers, there
8 * are no serial ports under the OS's control, and sometimes there is no other
9 * console available either. However, the service processor has two standard
10 * serial ports, so this over-complicated protocol allows the OS to control
11 * those ports by proxy.
12 *
13 * Besides data, the procotol supports the reading/writing of the serial
14 * port's DTR line, and the reading of the CD line. This is to allow the OS to
15 * control a modem attached to the service processor's serial port. Note that
16 * the OS cannot change the speed of the port through this protocol.
17 */
18
19#undef DEBUG
20
21#include <linux/console.h>
22#include <linux/ctype.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/module.h>
27#include <linux/major.h>
28#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/spinlock.h>
30#include <linux/sysrq.h>
31#include <linux/tty.h>
32#include <linux/tty_flip.h>
33#include <asm/hvcall.h>
34#include <asm/hvconsole.h>
35#include <asm/prom.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080036#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/vio.h>
38#include <asm/param.h>
Benjamin Herrenschmidt725e7892011-06-16 15:08:12 +000039#include <asm/hvsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define HVSI_MAJOR 229
42#define HVSI_MINOR 128
43#define MAX_NR_HVSI_CONSOLES 4
44
45#define HVSI_TIMEOUT (5*HZ)
46#define HVSI_VERSION 1
47#define HVSI_MAX_PACKET 256
48#define HVSI_MAX_READ 16
49#define HVSI_MAX_OUTGOING_DATA 12
50#define N_OUTBUF 12
51
52/*
53 * we pass data via two 8-byte registers, so we would like our char arrays
54 * properly aligned for those loads.
55 */
56#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
57
58struct hvsi_struct {
Jiri Slabyd73a4e72012-04-02 13:54:28 +020059 struct tty_port port;
David Howellsc4028952006-11-22 14:57:56 +000060 struct delayed_work writer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct work_struct handshaker;
62 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
63 wait_queue_head_t stateq; /* woken when HVSI state changes */
64 spinlock_t lock;
65 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 uint8_t throttle_buf[128];
67 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
68 /* inbuf is for packet reassembly. leave a little room for leftovers. */
69 uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
70 uint8_t *inbuf_end;
71 int n_throttle;
72 int n_outbuf;
73 uint32_t vtermno;
74 uint32_t virq;
75 atomic_t seqno; /* HVSI packet sequence number */
76 uint16_t mctrl;
77 uint8_t state; /* HVSI protocol state */
78 uint8_t flags;
79#ifdef CONFIG_MAGIC_SYSRQ
80 uint8_t sysrq;
81#endif /* CONFIG_MAGIC_SYSRQ */
82};
83static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
84
85static struct tty_driver *hvsi_driver;
86static int hvsi_count;
87static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
88
89enum HVSI_PROTOCOL_STATE {
90 HVSI_CLOSED,
91 HVSI_WAIT_FOR_VER_RESPONSE,
92 HVSI_WAIT_FOR_VER_QUERY,
93 HVSI_OPEN,
94 HVSI_WAIT_FOR_MCTRL_RESPONSE,
95 HVSI_FSP_DIED,
96};
97#define HVSI_CONSOLE 0x1
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static inline int is_console(struct hvsi_struct *hp)
100{
101 return hp->flags & HVSI_CONSOLE;
102}
103
104static inline int is_open(struct hvsi_struct *hp)
105{
106 /* if we're waiting for an mctrl then we're already open */
107 return (hp->state == HVSI_OPEN)
108 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
109}
110
111static inline void print_state(struct hvsi_struct *hp)
112{
113#ifdef DEBUG
114 static const char *state_names[] = {
115 "HVSI_CLOSED",
116 "HVSI_WAIT_FOR_VER_RESPONSE",
117 "HVSI_WAIT_FOR_VER_QUERY",
118 "HVSI_OPEN",
119 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
120 "HVSI_FSP_DIED",
121 };
Phil Carmody08a82c62010-05-24 14:33:04 -0700122 const char *name = (hp->state < ARRAY_SIZE(state_names))
123 ? state_names[hp->state] : "UNKNOWN";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 pr_debug("hvsi%i: state = %s\n", hp->index, name);
126#endif /* DEBUG */
127}
128
129static inline void __set_state(struct hvsi_struct *hp, int state)
130{
131 hp->state = state;
132 print_state(hp);
133 wake_up_all(&hp->stateq);
134}
135
136static inline void set_state(struct hvsi_struct *hp, int state)
137{
138 unsigned long flags;
139
140 spin_lock_irqsave(&hp->lock, flags);
141 __set_state(hp, state);
142 spin_unlock_irqrestore(&hp->lock, flags);
143}
144
145static inline int len_packet(const uint8_t *packet)
146{
147 return (int)((struct hvsi_header *)packet)->len;
148}
149
150static inline int is_header(const uint8_t *packet)
151{
152 struct hvsi_header *header = (struct hvsi_header *)packet;
153 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
154}
155
156static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
157{
158 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
159 return 0; /* don't even have the packet header */
160
161 if (hp->inbuf_end < (packet + len_packet(packet)))
162 return 0; /* don't have the rest of the packet */
163
164 return 1;
165}
166
167/* shift remaining bytes in packetbuf down */
168static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
169{
170 int remaining = (int)(hp->inbuf_end - read_to);
171
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700172 pr_debug("%s: %i chars remain\n", __func__, remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (read_to != hp->inbuf)
175 memmove(hp->inbuf, read_to, remaining);
176
177 hp->inbuf_end = hp->inbuf + remaining;
178}
179
180#ifdef DEBUG
181#define dbg_dump_packet(packet) dump_packet(packet)
182#define dbg_dump_hex(data, len) dump_hex(data, len)
183#else
184#define dbg_dump_packet(packet) do { } while (0)
185#define dbg_dump_hex(data, len) do { } while (0)
186#endif
187
188static void dump_hex(const uint8_t *data, int len)
189{
190 int i;
191
192 printk(" ");
193 for (i=0; i < len; i++)
194 printk("%.2x", data[i]);
195
196 printk("\n ");
197 for (i=0; i < len; i++) {
198 if (isprint(data[i]))
199 printk("%c", data[i]);
200 else
201 printk(".");
202 }
203 printk("\n");
204}
205
206static void dump_packet(uint8_t *packet)
207{
208 struct hvsi_header *header = (struct hvsi_header *)packet;
209
210 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
211 header->seqno);
212
213 dump_hex(packet, header->len);
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
217{
218 unsigned long got;
219
Milton Miller88de0be2005-07-07 17:56:27 -0700220 got = hvc_get_chars(hp->vtermno, buf, count);
221
222 return got;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
Jiri Slaby28c04472012-04-02 13:54:29 +0200226 struct tty_struct *tty, struct hvsi_struct **to_handshake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct hvsi_control *header = (struct hvsi_control *)packet;
229
Laurent Dufour48079802015-07-31 11:29:50 +0200230 switch (be16_to_cpu(header->verb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 case VSV_MODEM_CTL_UPDATE:
Laurent Dufour48079802015-07-31 11:29:50 +0200232 if ((be32_to_cpu(header->word) & HVSI_TSCD) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* CD went away; no more connection */
234 pr_debug("hvsi%i: CD dropped\n", hp->index);
235 hp->mctrl &= TIOCM_CD;
Jiri Slaby28c04472012-04-02 13:54:29 +0200236 if (tty && !C_CLOCAL(tty))
237 tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 break;
240 case VSV_CLOSE_PROTOCOL:
241 pr_debug("hvsi%i: service processor came back\n", hp->index);
242 if (hp->state != HVSI_CLOSED) {
243 *to_handshake = hp;
244 }
245 break;
246 default:
247 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
248 hp->index);
249 dump_packet(packet);
250 break;
251 }
252}
253
254static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
255{
256 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
Laurent Dufour48079802015-07-31 11:29:50 +0200257 uint32_t mctrl_word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 switch (hp->state) {
260 case HVSI_WAIT_FOR_VER_RESPONSE:
261 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
262 break;
263 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
264 hp->mctrl = 0;
Laurent Dufour48079802015-07-31 11:29:50 +0200265 mctrl_word = be32_to_cpu(resp->u.mctrl_word);
266 if (mctrl_word & HVSI_TSDTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 hp->mctrl |= TIOCM_DTR;
Laurent Dufour48079802015-07-31 11:29:50 +0200268 if (mctrl_word & HVSI_TSCD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 hp->mctrl |= TIOCM_CD;
270 __set_state(hp, HVSI_OPEN);
271 break;
272 default:
273 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
274 dump_packet(packet);
275 break;
276 }
277}
278
279/* respond to service processor's version query */
280static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
281{
282 struct hvsi_query_response packet __ALIGNED__;
283 int wrote;
284
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000285 packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
286 packet.hdr.len = sizeof(struct hvsi_query_response);
Laurent Dufour48079802015-07-31 11:29:50 +0200287 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
288 packet.verb = cpu_to_be16(VSV_SEND_VERSION_NUMBER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 packet.u.version = HVSI_VERSION;
Laurent Dufour48079802015-07-31 11:29:50 +0200290 packet.query_seqno = cpu_to_be16(query_seqno+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000292 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
293 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000295 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
296 if (wrote != packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
298 hp->index);
299 return -EIO;
300 }
301
302 return 0;
303}
304
305static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
306{
307 struct hvsi_query *query = (struct hvsi_query *)packet;
308
309 switch (hp->state) {
310 case HVSI_WAIT_FOR_VER_QUERY:
Laurent Dufour48079802015-07-31 11:29:50 +0200311 hvsi_version_respond(hp, be16_to_cpu(query->hdr.seqno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 __set_state(hp, HVSI_OPEN);
313 break;
314 default:
315 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
316 dump_packet(packet);
317 break;
318 }
319}
320
Jiri Slaby92a19f92013-01-03 15:53:03 +0100321static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int i;
324
325 for (i=0; i < len; i++) {
326 char c = buf[i];
327#ifdef CONFIG_MAGIC_SYSRQ
328 if (c == '\0') {
329 hp->sysrq = 1;
330 continue;
331 } else if (hp->sysrq) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700332 handle_sysrq(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 hp->sysrq = 0;
334 continue;
335 }
336#endif /* CONFIG_MAGIC_SYSRQ */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100337 tty_insert_flip_char(&hp->port, c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339}
340
341/*
342 * We could get 252 bytes of data at once here. But the tty layer only
343 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
344 * it. Accordingly we won't send more than 128 bytes at a time to the flip
345 * buffer, which will give the tty buffer a chance to throttle us. Should the
346 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
347 * revisited.
348 */
349#define TTY_THRESHOLD_THROTTLE 128
Jiri Slaby92a19f92013-01-03 15:53:03 +0100350static bool hvsi_recv_data(struct hvsi_struct *hp, const uint8_t *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 const struct hvsi_header *header = (const struct hvsi_header *)packet;
353 const uint8_t *data = packet + sizeof(struct hvsi_header);
354 int datalen = header->len - sizeof(struct hvsi_header);
355 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
356
357 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
358
359 if (datalen == 0)
Jiri Slaby28c04472012-04-02 13:54:29 +0200360 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (overflow > 0) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700363 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 datalen = TTY_THRESHOLD_THROTTLE;
365 }
366
Jiri Slaby92a19f92013-01-03 15:53:03 +0100367 hvsi_insert_chars(hp, data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (overflow > 0) {
370 /*
371 * we still have more data to deliver, so we need to save off the
372 * overflow and send it later
373 */
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700374 pr_debug("%s: deferring overflow\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
376 hp->n_throttle = overflow;
377 }
378
Jiri Slaby28c04472012-04-02 13:54:29 +0200379 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382/*
383 * Returns true/false indicating data successfully read from hypervisor.
384 * Used both to get packets for tty connections and to advance the state
385 * machine during console handshaking (in which case tty = NULL and we ignore
386 * incoming data).
387 */
Jiri Slaby28c04472012-04-02 13:54:29 +0200388static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty,
389 struct hvsi_struct **handshake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 uint8_t *packet = hp->inbuf;
392 int chunklen;
Jiri Slaby28c04472012-04-02 13:54:29 +0200393 bool flip = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *handshake = NULL;
396
397 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
398 if (chunklen == 0) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700399 pr_debug("%s: 0-length read\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401 }
402
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700403 pr_debug("%s: got %i bytes\n", __func__, chunklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dbg_dump_hex(hp->inbuf_end, chunklen);
405
406 hp->inbuf_end += chunklen;
407
408 /* handle all completed packets */
409 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
410 struct hvsi_header *header = (struct hvsi_header *)packet;
411
412 if (!is_header(packet)) {
413 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
414 /* skip bytes until we find a header or run out of data */
415 while ((packet < hp->inbuf_end) && (!is_header(packet)))
416 packet++;
417 continue;
418 }
419
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700420 pr_debug("%s: handling %i-byte packet\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 len_packet(packet));
422 dbg_dump_packet(packet);
423
424 switch (header->type) {
425 case VS_DATA_PACKET_HEADER:
426 if (!is_open(hp))
427 break;
Jiri Slaby92a19f92013-01-03 15:53:03 +0100428 flip = hvsi_recv_data(hp, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 break;
430 case VS_CONTROL_PACKET_HEADER:
Jiri Slaby28c04472012-04-02 13:54:29 +0200431 hvsi_recv_control(hp, packet, tty, handshake);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 break;
433 case VS_QUERY_RESPONSE_PACKET_HEADER:
434 hvsi_recv_response(hp, packet);
435 break;
436 case VS_QUERY_PACKET_HEADER:
437 hvsi_recv_query(hp, packet);
438 break;
439 default:
440 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
441 hp->index, header->type);
442 dump_packet(packet);
443 break;
444 }
445
446 packet += len_packet(packet);
447
Jiri Slaby28c04472012-04-02 13:54:29 +0200448 if (*handshake) {
449 pr_debug("%s: handshake\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 }
452 }
453
454 compact_inbuf(hp, packet);
455
Jiri Slaby28c04472012-04-02 13:54:29 +0200456 if (flip)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100457 tty_flip_buffer_push(&hp->port);
Jiri Slaby28c04472012-04-02 13:54:29 +0200458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 1;
460}
461
Jiri Slaby92a19f92013-01-03 15:53:03 +0100462static void hvsi_send_overflow(struct hvsi_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700464 pr_debug("%s: delivering %i bytes overflow\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 hp->n_throttle);
466
Jiri Slaby92a19f92013-01-03 15:53:03 +0100467 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 hp->n_throttle = 0;
469}
470
471/*
472 * must get all pending data because we only get an irq on empty->non-empty
473 * transition
474 */
David Howells7d12e782006-10-05 14:55:46 +0100475static irqreturn_t hvsi_interrupt(int irq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct hvsi_struct *handshake;
Jiri Slabydaea4402012-04-02 13:54:30 +0200479 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 unsigned long flags;
481 int again = 1;
482
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700483 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jiri Slabydaea4402012-04-02 13:54:30 +0200485 tty = tty_port_tty_get(&hp->port);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 while (again) {
488 spin_lock_irqsave(&hp->lock, flags);
Jiri Slabydaea4402012-04-02 13:54:30 +0200489 again = hvsi_load_chunk(hp, tty, &handshake);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 spin_unlock_irqrestore(&hp->lock, flags);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (handshake) {
493 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
494 schedule_work(&handshake->handshaker);
495 }
496 }
497
498 spin_lock_irqsave(&hp->lock, flags);
Peter Hurley97ef38b2016-04-09 17:11:36 -0700499 if (tty && hp->n_throttle && !tty_throttled(tty)) {
Jiri Slabydaea4402012-04-02 13:54:30 +0200500 /* we weren't hung up and we weren't throttled, so we can
501 * deliver the rest now */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100502 hvsi_send_overflow(hp);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100503 tty_flip_buffer_push(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 spin_unlock_irqrestore(&hp->lock, flags);
506
Jiri Slabydaea4402012-04-02 13:54:30 +0200507 tty_kref_put(tty);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return IRQ_HANDLED;
510}
511
512/* for boot console, before the irq handler is running */
513static int __init poll_for_state(struct hvsi_struct *hp, int state)
514{
515 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
516
517 for (;;) {
David Howells7d12e782006-10-05 14:55:46 +0100518 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (hp->state == state)
521 return 0;
522
523 mdelay(5);
524 if (time_after(jiffies, end_jiffies))
525 return -EIO;
526 }
527}
528
529/* wait for irq handler to change our state */
530static int wait_for_state(struct hvsi_struct *hp, int state)
531{
532 int ret = 0;
533
534 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
535 ret = -EIO;
536
537 return ret;
538}
539
540static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
541{
542 struct hvsi_query packet __ALIGNED__;
543 int wrote;
544
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000545 packet.hdr.type = VS_QUERY_PACKET_HEADER;
546 packet.hdr.len = sizeof(struct hvsi_query);
Laurent Dufour48079802015-07-31 11:29:50 +0200547 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
548 packet.verb = cpu_to_be16(verb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000550 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
551 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000553 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
554 if (wrote != packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
556 wrote);
557 return -EIO;
558 }
559
560 return 0;
561}
562
563static int hvsi_get_mctrl(struct hvsi_struct *hp)
564{
565 int ret;
566
567 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
568 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
569
570 ret = hvsi_wait(hp, HVSI_OPEN);
571 if (ret < 0) {
572 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
573 set_state(hp, HVSI_OPEN);
574 return ret;
575 }
576
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700577 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 return 0;
580}
581
582/* note that we can only set DTR */
583static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
584{
585 struct hvsi_control packet __ALIGNED__;
586 int wrote;
587
Laurent Dufour48079802015-07-31 11:29:50 +0200588 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
589 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000590 packet.hdr.len = sizeof(struct hvsi_control);
Laurent Dufour48079802015-07-31 11:29:50 +0200591 packet.verb = cpu_to_be16(VSV_SET_MODEM_CTL);
592 packet.mask = cpu_to_be32(HVSI_TSDTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (mctrl & TIOCM_DTR)
Laurent Dufour48079802015-07-31 11:29:50 +0200595 packet.word = cpu_to_be32(HVSI_TSDTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000597 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
598 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000600 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
601 if (wrote != packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
603 return -EIO;
604 }
605
606 return 0;
607}
608
609static void hvsi_drain_input(struct hvsi_struct *hp)
610{
611 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
612 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
613
614 while (time_before(end_jiffies, jiffies))
615 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
616 break;
617}
618
619static int hvsi_handshake(struct hvsi_struct *hp)
620{
621 int ret;
622
623 /*
624 * We could have a CLOSE or other data waiting for us before we even try
625 * to open; try to throw it all away so we don't get confused. (CLOSE
626 * is the first message sent up the pipe when the FSP comes online. We
627 * need to distinguish between "it came up a while ago and we're the first
628 * user" and "it was just reset before it saw our handshake packet".)
629 */
630 hvsi_drain_input(hp);
631
632 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
633 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
634 if (ret < 0) {
635 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
636 return ret;
637 }
638
639 ret = hvsi_wait(hp, HVSI_OPEN);
640 if (ret < 0)
641 return ret;
642
643 return 0;
644}
645
David Howellsc4028952006-11-22 14:57:56 +0000646static void hvsi_handshaker(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
David Howellsc4028952006-11-22 14:57:56 +0000648 struct hvsi_struct *hp =
649 container_of(work, struct hvsi_struct, handshaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (hvsi_handshake(hp) >= 0)
652 return;
653
654 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
655 if (is_console(hp)) {
656 /*
657 * ttys will re-attempt the handshake via hvsi_open, but
658 * the console will not.
659 */
660 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
661 }
662}
663
664static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
665{
666 struct hvsi_data packet __ALIGNED__;
667 int ret;
668
669 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
670
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000671 packet.hdr.type = VS_DATA_PACKET_HEADER;
Laurent Dufour48079802015-07-31 11:29:50 +0200672 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000673 packet.hdr.len = count + sizeof(struct hvsi_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 memcpy(&packet.data, buf, count);
675
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000676 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
677 if (ret == packet.hdr.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* return the number of chars written, not the packet length */
679 return count;
680 }
681 return ret; /* return any errors */
682}
683
684static void hvsi_close_protocol(struct hvsi_struct *hp)
685{
686 struct hvsi_control packet __ALIGNED__;
687
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000688 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
Laurent Dufour48079802015-07-31 11:29:50 +0200689 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000690 packet.hdr.len = 6;
Laurent Dufour48079802015-07-31 11:29:50 +0200691 packet.verb = cpu_to_be16(VSV_CLOSE_PROTOCOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000693 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
694 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Benjamin Herrenschmidt048bee72011-06-16 15:08:24 +0000696 hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699static int hvsi_open(struct tty_struct *tty, struct file *filp)
700{
701 struct hvsi_struct *hp;
702 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int ret;
704
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700705 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jiri Slaby410235f2012-03-05 14:52:01 +0100707 hp = &hvsi_ports[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 tty->driver_data = hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 mb();
712 if (hp->state == HVSI_FSP_DIED)
713 return -EIO;
714
Jiri Slabydaea4402012-04-02 13:54:30 +0200715 tty_port_tty_set(&hp->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 spin_lock_irqsave(&hp->lock, flags);
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200717 hp->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 atomic_set(&hp->seqno, 0);
719 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
720 spin_unlock_irqrestore(&hp->lock, flags);
721
722 if (is_console(hp))
723 return 0; /* this has already been handshaked as the console */
724
725 ret = hvsi_handshake(hp);
726 if (ret < 0) {
727 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
728 return ret;
729 }
730
731 ret = hvsi_get_mctrl(hp);
732 if (ret < 0) {
733 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
734 return ret;
735 }
736
737 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
738 if (ret < 0) {
739 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
740 return ret;
741 }
742
743 return 0;
744}
745
746/* wait for hvsi_write_worker to empty hp->outbuf */
747static void hvsi_flush_output(struct hvsi_struct *hp)
748{
749 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
750
751 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
Tejun Heo42565992010-12-24 15:59:07 +0100752 cancel_delayed_work_sync(&hp->writer);
Tejun Heo43829732012-08-20 14:51:24 -0700753 flush_work(&hp->handshaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /*
756 * it's also possible that our timeout expired and hvsi_write_worker
757 * didn't manage to push outbuf. poof.
758 */
759 hp->n_outbuf = 0;
760}
761
762static void hvsi_close(struct tty_struct *tty, struct file *filp)
763{
764 struct hvsi_struct *hp = tty->driver_data;
765 unsigned long flags;
766
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700767 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (tty_hung_up_p(filp))
770 return;
771
772 spin_lock_irqsave(&hp->lock, flags);
773
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200774 if (--hp->port.count == 0) {
Jiri Slabydaea4402012-04-02 13:54:30 +0200775 tty_port_tty_set(&hp->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
777
778 /* only close down connection if it is not the console */
779 if (!is_console(hp)) {
780 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
781 __set_state(hp, HVSI_CLOSED);
782 /*
783 * any data delivered to the tty layer after this will be
784 * discarded (except for XON/XOFF)
785 */
786 tty->closing = 1;
787
788 spin_unlock_irqrestore(&hp->lock, flags);
789
790 /* let any existing irq handlers finish. no more will start. */
791 synchronize_irq(hp->virq);
792
793 /* hvsi_write_worker will re-schedule until outbuf is empty. */
794 hvsi_flush_output(hp);
795
796 /* tell FSP to stop sending data */
797 hvsi_close_protocol(hp);
798
799 /*
800 * drain anything FSP is still in the middle of sending, and let
801 * hvsi_handshake drain the rest on the next open.
802 */
803 hvsi_drain_input(hp);
804
805 spin_lock_irqsave(&hp->lock, flags);
806 }
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200807 } else if (hp->port.count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200809 hp - hvsi_ports, hp->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 spin_unlock_irqrestore(&hp->lock, flags);
812}
813
814static void hvsi_hangup(struct tty_struct *tty)
815{
816 struct hvsi_struct *hp = tty->driver_data;
817 unsigned long flags;
818
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700819 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Jiri Slabydaea4402012-04-02 13:54:30 +0200821 tty_port_tty_set(&hp->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Jiri Slabydaea4402012-04-02 13:54:30 +0200823 spin_lock_irqsave(&hp->lock, flags);
Jiri Slabyd73a4e72012-04-02 13:54:28 +0200824 hp->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 hp->n_outbuf = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 spin_unlock_irqrestore(&hp->lock, flags);
827}
828
829/* called with hp->lock held */
830static void hvsi_push(struct hvsi_struct *hp)
831{
832 int n;
833
834 if (hp->n_outbuf <= 0)
835 return;
836
837 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
838 if (n > 0) {
839 /* success */
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700840 pr_debug("%s: wrote %i chars\n", __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 hp->n_outbuf = 0;
842 } else if (n == -EIO) {
843 __set_state(hp, HVSI_FSP_DIED);
844 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
845 }
846}
847
848/* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
David Howellsc4028952006-11-22 14:57:56 +0000849static void hvsi_write_worker(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
David Howellsc4028952006-11-22 14:57:56 +0000851 struct hvsi_struct *hp =
852 container_of(work, struct hvsi_struct, writer.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 unsigned long flags;
854#ifdef DEBUG
855 static long start_j = 0;
856
857 if (start_j == 0)
858 start_j = jiffies;
859#endif /* DEBUG */
860
861 spin_lock_irqsave(&hp->lock, flags);
862
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700863 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 if (!is_open(hp)) {
866 /*
867 * We could have a non-open connection if the service processor died
868 * while we were busily scheduling ourselves. In that case, it could
869 * be minutes before the service processor comes back, so only try
870 * again once a second.
871 */
872 schedule_delayed_work(&hp->writer, HZ);
873 goto out;
874 }
875
876 hvsi_push(hp);
877 if (hp->n_outbuf > 0)
878 schedule_delayed_work(&hp->writer, 10);
879 else {
880#ifdef DEBUG
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700881 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 jiffies - start_j);
883 start_j = 0;
884#endif /* DEBUG */
885 wake_up_all(&hp->emptyq);
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100886 tty_port_tty_wakeup(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
889out:
890 spin_unlock_irqrestore(&hp->lock, flags);
891}
892
893static int hvsi_write_room(struct tty_struct *tty)
894{
Alan Coxc9f19e92009-01-02 13:47:26 +0000895 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 return N_OUTBUF - hp->n_outbuf;
898}
899
900static int hvsi_chars_in_buffer(struct tty_struct *tty)
901{
Alan Coxc9f19e92009-01-02 13:47:26 +0000902 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return hp->n_outbuf;
905}
906
907static int hvsi_write(struct tty_struct *tty,
908 const unsigned char *buf, int count)
909{
910 struct hvsi_struct *hp = tty->driver_data;
911 const char *source = buf;
912 unsigned long flags;
913 int total = 0;
914 int origcount = count;
915
916 spin_lock_irqsave(&hp->lock, flags);
917
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700918 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (!is_open(hp)) {
921 /* we're either closing or not yet open; don't accept data */
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700922 pr_debug("%s: not open\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 goto out;
924 }
925
926 /*
927 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
928 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
929 * will see there is no room in outbuf and return.
930 */
Jiri Slaby28c04472012-04-02 13:54:29 +0200931 while ((count > 0) && (hvsi_write_room(tty) > 0)) {
932 int chunksize = min(count, hvsi_write_room(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 BUG_ON(hp->n_outbuf < 0);
935 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
936 hp->n_outbuf += chunksize;
937
938 total += chunksize;
939 source += chunksize;
940 count -= chunksize;
941 hvsi_push(hp);
942 }
943
944 if (hp->n_outbuf > 0) {
945 /*
946 * we weren't able to write it all to the hypervisor.
947 * schedule another push attempt.
948 */
949 schedule_delayed_work(&hp->writer, 10);
950 }
951
952out:
953 spin_unlock_irqrestore(&hp->lock, flags);
954
955 if (total != origcount)
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700956 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 total);
958
959 return total;
960}
961
962/*
963 * I have never seen throttle or unthrottle called, so this little throttle
964 * buffering scheme may or may not work.
965 */
966static void hvsi_throttle(struct tty_struct *tty)
967{
Alan Coxc9f19e92009-01-02 13:47:26 +0000968 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700970 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
973}
974
975static void hvsi_unthrottle(struct tty_struct *tty)
976{
Alan Coxc9f19e92009-01-02 13:47:26 +0000977 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700980 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 spin_lock_irqsave(&hp->lock, flags);
983 if (hp->n_throttle) {
Jiri Slaby92a19f92013-01-03 15:53:03 +0100984 hvsi_send_overflow(hp);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100985 tty_flip_buffer_push(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 spin_unlock_irqrestore(&hp->lock, flags);
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
991}
992
Alan Cox60b33c12011-02-14 16:26:14 +0000993static int hvsi_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Alan Coxc9f19e92009-01-02 13:47:26 +0000995 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 hvsi_get_mctrl(hp);
998 return hp->mctrl;
999}
1000
Alan Cox20b9d172011-02-14 16:26:50 +00001001static int hvsi_tiocmset(struct tty_struct *tty,
1002 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Alan Coxc9f19e92009-01-02 13:47:26 +00001004 struct hvsi_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 unsigned long flags;
1006 uint16_t new_mctrl;
1007
1008 /* we can only alter DTR */
1009 clear &= TIOCM_DTR;
1010 set &= TIOCM_DTR;
1011
1012 spin_lock_irqsave(&hp->lock, flags);
1013
1014 new_mctrl = (hp->mctrl & ~clear) | set;
1015
1016 if (hp->mctrl != new_mctrl) {
1017 hvsi_set_mctrl(hp, new_mctrl);
1018 hp->mctrl = new_mctrl;
1019 }
1020 spin_unlock_irqrestore(&hp->lock, flags);
1021
1022 return 0;
1023}
1024
1025
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001026static const struct tty_operations hvsi_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 .open = hvsi_open,
1028 .close = hvsi_close,
1029 .write = hvsi_write,
1030 .hangup = hvsi_hangup,
1031 .write_room = hvsi_write_room,
1032 .chars_in_buffer = hvsi_chars_in_buffer,
1033 .throttle = hvsi_throttle,
1034 .unthrottle = hvsi_unthrottle,
1035 .tiocmget = hvsi_tiocmget,
1036 .tiocmset = hvsi_tiocmset,
1037};
1038
1039static int __init hvsi_init(void)
1040{
1041 int i;
1042
1043 hvsi_driver = alloc_tty_driver(hvsi_count);
1044 if (!hvsi_driver)
1045 return -ENOMEM;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 hvsi_driver->driver_name = "hvsi";
1048 hvsi_driver->name = "hvsi";
1049 hvsi_driver->major = HVSI_MAJOR;
1050 hvsi_driver->minor_start = HVSI_MINOR;
1051 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1052 hvsi_driver->init_termios = tty_std_termios;
1053 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001054 hvsi_driver->init_termios.c_ispeed = 9600;
1055 hvsi_driver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1057 tty_set_operations(hvsi_driver, &hvsi_ops);
1058
1059 for (i=0; i < hvsi_count; i++) {
1060 struct hvsi_struct *hp = &hvsi_ports[i];
1061 int ret = 1;
1062
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001063 tty_port_link_device(&hp->port, hvsi_driver, i);
1064
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001065 ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (ret)
1067 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1068 hp->virq, ret);
1069 }
1070 hvsi_wait = wait_for_state; /* irqs active now */
1071
1072 if (tty_register_driver(hvsi_driver))
1073 panic("Couldn't register hvsi console driver\n");
1074
Olof Johansson8b6a7b22006-04-12 15:19:50 -05001075 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 return 0;
1078}
1079device_initcall(hvsi_init);
1080
1081/***** console (not tty) code: *****/
1082
1083static void hvsi_console_print(struct console *console, const char *buf,
1084 unsigned int count)
1085{
1086 struct hvsi_struct *hp = &hvsi_ports[console->index];
1087 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1088 unsigned int i = 0, n = 0;
1089 int ret, donecr = 0;
1090
1091 mb();
1092 if (!is_open(hp))
1093 return;
1094
1095 /*
1096 * ugh, we have to translate LF -> CRLF ourselves, in place.
1097 * copied from hvc_console.c:
1098 */
1099 while (count > 0 || i > 0) {
1100 if (count > 0 && i < sizeof(c)) {
1101 if (buf[n] == '\n' && !donecr) {
1102 c[i++] = '\r';
1103 donecr = 1;
1104 } else {
1105 c[i++] = buf[n++];
1106 donecr = 0;
1107 --count;
1108 }
1109 } else {
1110 ret = hvsi_put_chars(hp, c, i);
1111 if (ret < 0)
1112 i = 0;
1113 i -= ret;
1114 }
1115 }
1116}
1117
1118static struct tty_driver *hvsi_console_device(struct console *console,
1119 int *index)
1120{
1121 *index = console->index;
1122 return hvsi_driver;
1123}
1124
1125static int __init hvsi_console_setup(struct console *console, char *options)
1126{
Roel Kluin72865652009-08-06 13:00:37 +00001127 struct hvsi_struct *hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int ret;
1129
1130 if (console->index < 0 || console->index >= hvsi_count)
1131 return -1;
Roel Kluin72865652009-08-06 13:00:37 +00001132 hp = &hvsi_ports[console->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* give the FSP a chance to change the baud rate when we re-open */
1135 hvsi_close_protocol(hp);
1136
1137 ret = hvsi_handshake(hp);
1138 if (ret < 0)
1139 return ret;
1140
1141 ret = hvsi_get_mctrl(hp);
1142 if (ret < 0)
1143 return ret;
1144
1145 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1146 if (ret < 0)
1147 return ret;
1148
1149 hp->flags |= HVSI_CONSOLE;
1150
1151 return 0;
1152}
1153
Chris Metcalf4455b112010-07-06 08:03:16 +00001154static struct console hvsi_console = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 .name = "hvsi",
1156 .write = hvsi_console_print,
1157 .device = hvsi_console_device,
1158 .setup = hvsi_console_setup,
1159 .flags = CON_PRINTBUFFER,
1160 .index = -1,
1161};
1162
1163static int __init hvsi_console_init(void)
1164{
1165 struct device_node *vty;
1166
1167 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1168
1169 /* search device tree for vty nodes */
Wei Yongjun074d35c2012-12-04 00:16:18 -05001170 for_each_compatible_node(vty, "serial", "hvterm-protocol") {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct hvsi_struct *hp;
Laurent Dufour48079802015-07-31 11:29:50 +02001172 const __be32 *vtermno, *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Stephen Rothwell01b27262007-04-27 13:41:15 +10001174 vtermno = of_get_property(vty, "reg", NULL);
1175 irq = of_get_property(vty, "interrupts", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (!vtermno || !irq)
1177 continue;
1178
1179 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1180 of_node_put(vty);
1181 break;
1182 }
1183
1184 hp = &hvsi_ports[hvsi_count];
David Howellsc4028952006-11-22 14:57:56 +00001185 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1186 INIT_WORK(&hp->handshaker, hvsi_handshaker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 init_waitqueue_head(&hp->emptyq);
1188 init_waitqueue_head(&hp->stateq);
1189 spin_lock_init(&hp->lock);
Jiri Slabyd73a4e72012-04-02 13:54:28 +02001190 tty_port_init(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 hp->index = hvsi_count;
1192 hp->inbuf_end = hp->inbuf;
1193 hp->state = HVSI_CLOSED;
Laurent Dufour48079802015-07-31 11:29:50 +02001194 hp->vtermno = be32_to_cpup(vtermno);
1195 hp->virq = irq_create_mapping(NULL, be32_to_cpup(irq));
Alan Coxd4e33fa2012-01-26 17:44:09 +00001196 if (hp->virq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
Laurent Dufour48079802015-07-31 11:29:50 +02001198 __func__, be32_to_cpup(irq));
Jiri Slaby191c5f12012-11-15 09:49:56 +01001199 tty_port_destroy(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 continue;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 hvsi_count++;
1204 }
1205
1206 if (hvsi_count)
Chris Metcalf4455b112010-07-06 08:03:16 +00001207 register_console(&hvsi_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return 0;
1209}
1210console_initcall(hvsi_console_init);