Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | /* Host Virtual Serial Interface (HVSI) is a protocol between the hosted OS |
| 20 | * and the service processor on IBM pSeries servers. On these servers, there |
| 21 | * are no serial ports under the OS's control, and sometimes there is no other |
| 22 | * console available either. However, the service processor has two standard |
| 23 | * serial ports, so this over-complicated protocol allows the OS to control |
| 24 | * those ports by proxy. |
| 25 | * |
| 26 | * Besides data, the procotol supports the reading/writing of the serial |
| 27 | * port's DTR line, and the reading of the CD line. This is to allow the OS to |
| 28 | * control a modem attached to the service processor's serial port. Note that |
| 29 | * the OS cannot change the speed of the port through this protocol. |
| 30 | */ |
| 31 | |
| 32 | #undef DEBUG |
| 33 | |
| 34 | #include <linux/console.h> |
| 35 | #include <linux/ctype.h> |
| 36 | #include <linux/delay.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <linux/interrupt.h> |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/major.h> |
| 41 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/spinlock.h> |
| 43 | #include <linux/sysrq.h> |
| 44 | #include <linux/tty.h> |
| 45 | #include <linux/tty_flip.h> |
| 46 | #include <asm/hvcall.h> |
| 47 | #include <asm/hvconsole.h> |
| 48 | #include <asm/prom.h> |
| 49 | #include <asm/uaccess.h> |
| 50 | #include <asm/vio.h> |
| 51 | #include <asm/param.h> |
Benjamin Herrenschmidt | 725e789 | 2011-06-16 15:08:12 +0000 | [diff] [blame] | 52 | #include <asm/hvsi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 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 | |
| 71 | struct hvsi_struct { |
Jiri Slaby | d73a4e7 | 2012-04-02 13:54:28 +0200 | [diff] [blame] | 72 | struct tty_port port; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 73 | struct delayed_work writer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | uint8_t throttle_buf[128]; |
| 80 | uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */ |
| 81 | /* inbuf is for packet reassembly. leave a little room for leftovers. */ |
| 82 | uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ]; |
| 83 | uint8_t *inbuf_end; |
| 84 | int n_throttle; |
| 85 | int n_outbuf; |
| 86 | uint32_t vtermno; |
| 87 | uint32_t virq; |
| 88 | atomic_t seqno; /* HVSI packet sequence number */ |
| 89 | uint16_t mctrl; |
| 90 | uint8_t state; /* HVSI protocol state */ |
| 91 | uint8_t flags; |
| 92 | #ifdef CONFIG_MAGIC_SYSRQ |
| 93 | uint8_t sysrq; |
| 94 | #endif /* CONFIG_MAGIC_SYSRQ */ |
| 95 | }; |
| 96 | static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES]; |
| 97 | |
| 98 | static struct tty_driver *hvsi_driver; |
| 99 | static int hvsi_count; |
| 100 | static int (*hvsi_wait)(struct hvsi_struct *hp, int state); |
| 101 | |
| 102 | enum 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | static inline int is_console(struct hvsi_struct *hp) |
| 113 | { |
| 114 | return hp->flags & HVSI_CONSOLE; |
| 115 | } |
| 116 | |
| 117 | static 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 | |
| 124 | static 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 Carmody | 08a82c6 | 2010-05-24 14:33:04 -0700 | [diff] [blame] | 135 | const char *name = (hp->state < ARRAY_SIZE(state_names)) |
| 136 | ? state_names[hp->state] : "UNKNOWN"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | pr_debug("hvsi%i: state = %s\n", hp->index, name); |
| 139 | #endif /* DEBUG */ |
| 140 | } |
| 141 | |
| 142 | static 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 | |
| 149 | static 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 | |
| 158 | static inline int len_packet(const uint8_t *packet) |
| 159 | { |
| 160 | return (int)((struct hvsi_header *)packet)->len; |
| 161 | } |
| 162 | |
| 163 | static 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 | |
| 169 | static 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 */ |
| 181 | static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to) |
| 182 | { |
| 183 | int remaining = (int)(hp->inbuf_end - read_to); |
| 184 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 185 | pr_debug("%s: %i chars remain\n", __func__, remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 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 | |
| 201 | static 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 | |
| 219 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | static int hvsi_read(struct hvsi_struct *hp, char *buf, int count) |
| 230 | { |
| 231 | unsigned long got; |
| 232 | |
Milton Miller | 88de0be | 2005-07-07 17:56:27 -0700 | [diff] [blame] | 233 | got = hvc_get_chars(hp->vtermno, buf, count); |
| 234 | |
| 235 | return got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet, |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 239 | struct tty_struct *tty, struct hvsi_struct **to_handshake) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
| 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 Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 249 | if (tty && !C_CLOCAL(tty)) |
| 250 | tty_hangup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 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 | |
| 267 | static 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 */ |
| 291 | static 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 Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 296 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | packet.verb = VSV_SEND_VERSION_NUMBER; |
| 300 | packet.u.version = HVSI_VERSION; |
| 301 | packet.query_seqno = query_seqno+1; |
| 302 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 303 | pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len); |
| 304 | dbg_dump_hex((uint8_t*)&packet, packet.hdr.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 306 | wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); |
| 307 | if (wrote != packet.hdr.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | printk(KERN_ERR "hvsi%i: couldn't send query response!\n", |
| 309 | hp->index); |
| 310 | return -EIO; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static 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 Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 322 | hvsi_version_respond(hp, query->hdr.seqno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | __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 Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 332 | static void hvsi_insert_chars(struct hvsi_struct *hp, struct tty_struct *tty, |
| 333 | const char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | int i; |
| 336 | |
| 337 | for (i=0; i < len; i++) { |
| 338 | char c = buf[i]; |
| 339 | #ifdef CONFIG_MAGIC_SYSRQ |
| 340 | if (c == '\0') { |
| 341 | hp->sysrq = 1; |
| 342 | continue; |
| 343 | } else if (hp->sysrq) { |
Dmitry Torokhov | f335397 | 2010-08-17 21:15:47 -0700 | [diff] [blame] | 344 | handle_sysrq(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | hp->sysrq = 0; |
| 346 | continue; |
| 347 | } |
| 348 | #endif /* CONFIG_MAGIC_SYSRQ */ |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 349 | tty_insert_flip_char(tty, c, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * We could get 252 bytes of data at once here. But the tty layer only |
| 355 | * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow |
| 356 | * it. Accordingly we won't send more than 128 bytes at a time to the flip |
| 357 | * buffer, which will give the tty buffer a chance to throttle us. Should the |
| 358 | * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be |
| 359 | * revisited. |
| 360 | */ |
| 361 | #define TTY_THRESHOLD_THROTTLE 128 |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 362 | static bool hvsi_recv_data(struct hvsi_struct *hp, struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | const uint8_t *packet) |
| 364 | { |
| 365 | const struct hvsi_header *header = (const struct hvsi_header *)packet; |
| 366 | const uint8_t *data = packet + sizeof(struct hvsi_header); |
| 367 | int datalen = header->len - sizeof(struct hvsi_header); |
| 368 | int overflow = datalen - TTY_THRESHOLD_THROTTLE; |
| 369 | |
| 370 | pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data); |
| 371 | |
| 372 | if (datalen == 0) |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 373 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | if (overflow > 0) { |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 376 | pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | datalen = TTY_THRESHOLD_THROTTLE; |
| 378 | } |
| 379 | |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 380 | hvsi_insert_chars(hp, tty, data, datalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | if (overflow > 0) { |
| 383 | /* |
| 384 | * we still have more data to deliver, so we need to save off the |
| 385 | * overflow and send it later |
| 386 | */ |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 387 | pr_debug("%s: deferring overflow\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow); |
| 389 | hp->n_throttle = overflow; |
| 390 | } |
| 391 | |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 392 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Returns true/false indicating data successfully read from hypervisor. |
| 397 | * Used both to get packets for tty connections and to advance the state |
| 398 | * machine during console handshaking (in which case tty = NULL and we ignore |
| 399 | * incoming data). |
| 400 | */ |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 401 | static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty, |
| 402 | struct hvsi_struct **handshake) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
| 404 | uint8_t *packet = hp->inbuf; |
| 405 | int chunklen; |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 406 | bool flip = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | *handshake = NULL; |
| 409 | |
| 410 | chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ); |
| 411 | if (chunklen == 0) { |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 412 | pr_debug("%s: 0-length read\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 416 | pr_debug("%s: got %i bytes\n", __func__, chunklen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | dbg_dump_hex(hp->inbuf_end, chunklen); |
| 418 | |
| 419 | hp->inbuf_end += chunklen; |
| 420 | |
| 421 | /* handle all completed packets */ |
| 422 | while ((packet < hp->inbuf_end) && got_packet(hp, packet)) { |
| 423 | struct hvsi_header *header = (struct hvsi_header *)packet; |
| 424 | |
| 425 | if (!is_header(packet)) { |
| 426 | printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index); |
| 427 | /* skip bytes until we find a header or run out of data */ |
| 428 | while ((packet < hp->inbuf_end) && (!is_header(packet))) |
| 429 | packet++; |
| 430 | continue; |
| 431 | } |
| 432 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 433 | pr_debug("%s: handling %i-byte packet\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | len_packet(packet)); |
| 435 | dbg_dump_packet(packet); |
| 436 | |
| 437 | switch (header->type) { |
| 438 | case VS_DATA_PACKET_HEADER: |
| 439 | if (!is_open(hp)) |
| 440 | break; |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 441 | if (tty == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | break; /* no tty buffer to put data in */ |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 443 | flip = hvsi_recv_data(hp, tty, packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | break; |
| 445 | case VS_CONTROL_PACKET_HEADER: |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 446 | hvsi_recv_control(hp, packet, tty, handshake); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | break; |
| 448 | case VS_QUERY_RESPONSE_PACKET_HEADER: |
| 449 | hvsi_recv_response(hp, packet); |
| 450 | break; |
| 451 | case VS_QUERY_PACKET_HEADER: |
| 452 | hvsi_recv_query(hp, packet); |
| 453 | break; |
| 454 | default: |
| 455 | printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n", |
| 456 | hp->index, header->type); |
| 457 | dump_packet(packet); |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | packet += len_packet(packet); |
| 462 | |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 463 | if (*handshake) { |
| 464 | pr_debug("%s: handshake\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | compact_inbuf(hp, packet); |
| 470 | |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 471 | if (flip) |
| 472 | tty_flip_buffer_push(tty); |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | return 1; |
| 475 | } |
| 476 | |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 477 | static void hvsi_send_overflow(struct hvsi_struct *hp, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 479 | pr_debug("%s: delivering %i bytes overflow\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | hp->n_throttle); |
| 481 | |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 482 | hvsi_insert_chars(hp, tty, hp->throttle_buf, hp->n_throttle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | hp->n_throttle = 0; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * must get all pending data because we only get an irq on empty->non-empty |
| 488 | * transition |
| 489 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 490 | static irqreturn_t hvsi_interrupt(int irq, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
| 492 | struct hvsi_struct *hp = (struct hvsi_struct *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | struct hvsi_struct *handshake; |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 494 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | unsigned long flags; |
| 496 | int again = 1; |
| 497 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 498 | pr_debug("%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 500 | tty = tty_port_tty_get(&hp->port); |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | while (again) { |
| 503 | spin_lock_irqsave(&hp->lock, flags); |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 504 | again = hvsi_load_chunk(hp, tty, &handshake); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | spin_unlock_irqrestore(&hp->lock, flags); |
| 506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if (handshake) { |
| 508 | pr_debug("hvsi%i: attempting re-handshake\n", handshake->index); |
| 509 | schedule_work(&handshake->handshaker); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | spin_lock_irqsave(&hp->lock, flags); |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 514 | if (tty && hp->n_throttle && !test_bit(TTY_THROTTLED, &tty->flags)) { |
| 515 | /* we weren't hung up and we weren't throttled, so we can |
| 516 | * deliver the rest now */ |
| 517 | hvsi_send_overflow(hp, tty); |
| 518 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | spin_unlock_irqrestore(&hp->lock, flags); |
| 521 | |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 522 | tty_kref_put(tty); |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | return IRQ_HANDLED; |
| 525 | } |
| 526 | |
| 527 | /* for boot console, before the irq handler is running */ |
| 528 | static int __init poll_for_state(struct hvsi_struct *hp, int state) |
| 529 | { |
| 530 | unsigned long end_jiffies = jiffies + HVSI_TIMEOUT; |
| 531 | |
| 532 | for (;;) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 533 | hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
| 535 | if (hp->state == state) |
| 536 | return 0; |
| 537 | |
| 538 | mdelay(5); |
| 539 | if (time_after(jiffies, end_jiffies)) |
| 540 | return -EIO; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /* wait for irq handler to change our state */ |
| 545 | static int wait_for_state(struct hvsi_struct *hp, int state) |
| 546 | { |
| 547 | int ret = 0; |
| 548 | |
| 549 | if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT)) |
| 550 | ret = -EIO; |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | static int hvsi_query(struct hvsi_struct *hp, uint16_t verb) |
| 556 | { |
| 557 | struct hvsi_query packet __ALIGNED__; |
| 558 | int wrote; |
| 559 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 560 | packet.hdr.type = VS_QUERY_PACKET_HEADER; |
| 561 | packet.hdr.len = sizeof(struct hvsi_query); |
| 562 | packet.hdr.seqno = atomic_inc_return(&hp->seqno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | packet.verb = verb; |
| 564 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 565 | pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len); |
| 566 | dbg_dump_hex((uint8_t*)&packet, packet.hdr.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 568 | wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); |
| 569 | if (wrote != packet.hdr.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index, |
| 571 | wrote); |
| 572 | return -EIO; |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int hvsi_get_mctrl(struct hvsi_struct *hp) |
| 579 | { |
| 580 | int ret; |
| 581 | |
| 582 | set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE); |
| 583 | hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS); |
| 584 | |
| 585 | ret = hvsi_wait(hp, HVSI_OPEN); |
| 586 | if (ret < 0) { |
| 587 | printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index); |
| 588 | set_state(hp, HVSI_OPEN); |
| 589 | return ret; |
| 590 | } |
| 591 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 592 | pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | /* note that we can only set DTR */ |
| 598 | static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl) |
| 599 | { |
| 600 | struct hvsi_control packet __ALIGNED__; |
| 601 | int wrote; |
| 602 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 603 | packet.hdr.type = VS_CONTROL_PACKET_HEADER, |
| 604 | packet.hdr.seqno = atomic_inc_return(&hp->seqno); |
| 605 | packet.hdr.len = sizeof(struct hvsi_control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | packet.verb = VSV_SET_MODEM_CTL; |
| 607 | packet.mask = HVSI_TSDTR; |
| 608 | |
| 609 | if (mctrl & TIOCM_DTR) |
| 610 | packet.word = HVSI_TSDTR; |
| 611 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 612 | pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len); |
| 613 | dbg_dump_hex((uint8_t*)&packet, packet.hdr.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 615 | wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); |
| 616 | if (wrote != packet.hdr.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index); |
| 618 | return -EIO; |
| 619 | } |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static void hvsi_drain_input(struct hvsi_struct *hp) |
| 625 | { |
| 626 | uint8_t buf[HVSI_MAX_READ] __ALIGNED__; |
| 627 | unsigned long end_jiffies = jiffies + HVSI_TIMEOUT; |
| 628 | |
| 629 | while (time_before(end_jiffies, jiffies)) |
| 630 | if (0 == hvsi_read(hp, buf, HVSI_MAX_READ)) |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | static int hvsi_handshake(struct hvsi_struct *hp) |
| 635 | { |
| 636 | int ret; |
| 637 | |
| 638 | /* |
| 639 | * We could have a CLOSE or other data waiting for us before we even try |
| 640 | * to open; try to throw it all away so we don't get confused. (CLOSE |
| 641 | * is the first message sent up the pipe when the FSP comes online. We |
| 642 | * need to distinguish between "it came up a while ago and we're the first |
| 643 | * user" and "it was just reset before it saw our handshake packet".) |
| 644 | */ |
| 645 | hvsi_drain_input(hp); |
| 646 | |
| 647 | set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE); |
| 648 | ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER); |
| 649 | if (ret < 0) { |
| 650 | printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index); |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | ret = hvsi_wait(hp, HVSI_OPEN); |
| 655 | if (ret < 0) |
| 656 | return ret; |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 661 | static void hvsi_handshaker(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 663 | struct hvsi_struct *hp = |
| 664 | container_of(work, struct hvsi_struct, handshaker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
| 666 | if (hvsi_handshake(hp) >= 0) |
| 667 | return; |
| 668 | |
| 669 | printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index); |
| 670 | if (is_console(hp)) { |
| 671 | /* |
| 672 | * ttys will re-attempt the handshake via hvsi_open, but |
| 673 | * the console will not. |
| 674 | */ |
| 675 | printk(KERN_ERR "hvsi%i: lost console!\n", hp->index); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count) |
| 680 | { |
| 681 | struct hvsi_data packet __ALIGNED__; |
| 682 | int ret; |
| 683 | |
| 684 | BUG_ON(count > HVSI_MAX_OUTGOING_DATA); |
| 685 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 686 | packet.hdr.type = VS_DATA_PACKET_HEADER; |
| 687 | packet.hdr.seqno = atomic_inc_return(&hp->seqno); |
| 688 | packet.hdr.len = count + sizeof(struct hvsi_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | memcpy(&packet.data, buf, count); |
| 690 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 691 | ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); |
| 692 | if (ret == packet.hdr.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | /* return the number of chars written, not the packet length */ |
| 694 | return count; |
| 695 | } |
| 696 | return ret; /* return any errors */ |
| 697 | } |
| 698 | |
| 699 | static void hvsi_close_protocol(struct hvsi_struct *hp) |
| 700 | { |
| 701 | struct hvsi_control packet __ALIGNED__; |
| 702 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 703 | packet.hdr.type = VS_CONTROL_PACKET_HEADER; |
| 704 | packet.hdr.seqno = atomic_inc_return(&hp->seqno); |
| 705 | packet.hdr.len = 6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | packet.verb = VSV_CLOSE_PROTOCOL; |
| 707 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 708 | pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len); |
| 709 | dbg_dump_hex((uint8_t*)&packet, packet.hdr.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
Benjamin Herrenschmidt | 048bee7 | 2011-06-16 15:08:24 +0000 | [diff] [blame] | 711 | hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static int hvsi_open(struct tty_struct *tty, struct file *filp) |
| 715 | { |
| 716 | struct hvsi_struct *hp; |
| 717 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | int ret; |
| 719 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 720 | pr_debug("%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Jiri Slaby | 410235f | 2012-03-05 14:52:01 +0100 | [diff] [blame] | 722 | hp = &hvsi_ports[tty->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
| 724 | tty->driver_data = hp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
| 726 | mb(); |
| 727 | if (hp->state == HVSI_FSP_DIED) |
| 728 | return -EIO; |
| 729 | |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 730 | tty_port_tty_set(&hp->port, tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | spin_lock_irqsave(&hp->lock, flags); |
Jiri Slaby | d73a4e7 | 2012-04-02 13:54:28 +0200 | [diff] [blame] | 732 | hp->port.count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | atomic_set(&hp->seqno, 0); |
| 734 | h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE); |
| 735 | spin_unlock_irqrestore(&hp->lock, flags); |
| 736 | |
| 737 | if (is_console(hp)) |
| 738 | return 0; /* this has already been handshaked as the console */ |
| 739 | |
| 740 | ret = hvsi_handshake(hp); |
| 741 | if (ret < 0) { |
| 742 | printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name); |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | ret = hvsi_get_mctrl(hp); |
| 747 | if (ret < 0) { |
| 748 | printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name); |
| 749 | return ret; |
| 750 | } |
| 751 | |
| 752 | ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR); |
| 753 | if (ret < 0) { |
| 754 | printk(KERN_ERR "%s: couldn't set DTR\n", tty->name); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | /* wait for hvsi_write_worker to empty hp->outbuf */ |
| 762 | static void hvsi_flush_output(struct hvsi_struct *hp) |
| 763 | { |
| 764 | wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT); |
| 765 | |
| 766 | /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */ |
Tejun Heo | 4256599 | 2010-12-24 15:59:07 +0100 | [diff] [blame] | 767 | cancel_delayed_work_sync(&hp->writer); |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 768 | flush_work(&hp->handshaker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | /* |
| 771 | * it's also possible that our timeout expired and hvsi_write_worker |
| 772 | * didn't manage to push outbuf. poof. |
| 773 | */ |
| 774 | hp->n_outbuf = 0; |
| 775 | } |
| 776 | |
| 777 | static void hvsi_close(struct tty_struct *tty, struct file *filp) |
| 778 | { |
| 779 | struct hvsi_struct *hp = tty->driver_data; |
| 780 | unsigned long flags; |
| 781 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 782 | pr_debug("%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
| 784 | if (tty_hung_up_p(filp)) |
| 785 | return; |
| 786 | |
| 787 | spin_lock_irqsave(&hp->lock, flags); |
| 788 | |
Jiri Slaby | d73a4e7 | 2012-04-02 13:54:28 +0200 | [diff] [blame] | 789 | if (--hp->port.count == 0) { |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 790 | tty_port_tty_set(&hp->port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */ |
| 792 | |
| 793 | /* only close down connection if it is not the console */ |
| 794 | if (!is_console(hp)) { |
| 795 | h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */ |
| 796 | __set_state(hp, HVSI_CLOSED); |
| 797 | /* |
| 798 | * any data delivered to the tty layer after this will be |
| 799 | * discarded (except for XON/XOFF) |
| 800 | */ |
| 801 | tty->closing = 1; |
| 802 | |
| 803 | spin_unlock_irqrestore(&hp->lock, flags); |
| 804 | |
| 805 | /* let any existing irq handlers finish. no more will start. */ |
| 806 | synchronize_irq(hp->virq); |
| 807 | |
| 808 | /* hvsi_write_worker will re-schedule until outbuf is empty. */ |
| 809 | hvsi_flush_output(hp); |
| 810 | |
| 811 | /* tell FSP to stop sending data */ |
| 812 | hvsi_close_protocol(hp); |
| 813 | |
| 814 | /* |
| 815 | * drain anything FSP is still in the middle of sending, and let |
| 816 | * hvsi_handshake drain the rest on the next open. |
| 817 | */ |
| 818 | hvsi_drain_input(hp); |
| 819 | |
| 820 | spin_lock_irqsave(&hp->lock, flags); |
| 821 | } |
Jiri Slaby | d73a4e7 | 2012-04-02 13:54:28 +0200 | [diff] [blame] | 822 | } else if (hp->port.count < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n", |
Jiri Slaby | d73a4e7 | 2012-04-02 13:54:28 +0200 | [diff] [blame] | 824 | hp - hvsi_ports, hp->port.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
| 826 | spin_unlock_irqrestore(&hp->lock, flags); |
| 827 | } |
| 828 | |
| 829 | static void hvsi_hangup(struct tty_struct *tty) |
| 830 | { |
| 831 | struct hvsi_struct *hp = tty->driver_data; |
| 832 | unsigned long flags; |
| 833 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 834 | pr_debug("%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 836 | tty_port_tty_set(&hp->port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 838 | spin_lock_irqsave(&hp->lock, flags); |
Jiri Slaby | d73a4e7 | 2012-04-02 13:54:28 +0200 | [diff] [blame] | 839 | hp->port.count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | hp->n_outbuf = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | spin_unlock_irqrestore(&hp->lock, flags); |
| 842 | } |
| 843 | |
| 844 | /* called with hp->lock held */ |
| 845 | static void hvsi_push(struct hvsi_struct *hp) |
| 846 | { |
| 847 | int n; |
| 848 | |
| 849 | if (hp->n_outbuf <= 0) |
| 850 | return; |
| 851 | |
| 852 | n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf); |
| 853 | if (n > 0) { |
| 854 | /* success */ |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 855 | pr_debug("%s: wrote %i chars\n", __func__, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | hp->n_outbuf = 0; |
| 857 | } else if (n == -EIO) { |
| 858 | __set_state(hp, HVSI_FSP_DIED); |
| 859 | printk(KERN_ERR "hvsi%i: service processor died\n", hp->index); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 864 | static void hvsi_write_worker(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 866 | struct hvsi_struct *hp = |
| 867 | container_of(work, struct hvsi_struct, writer.work); |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 868 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | unsigned long flags; |
| 870 | #ifdef DEBUG |
| 871 | static long start_j = 0; |
| 872 | |
| 873 | if (start_j == 0) |
| 874 | start_j = jiffies; |
| 875 | #endif /* DEBUG */ |
| 876 | |
| 877 | spin_lock_irqsave(&hp->lock, flags); |
| 878 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 879 | pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
| 881 | if (!is_open(hp)) { |
| 882 | /* |
| 883 | * We could have a non-open connection if the service processor died |
| 884 | * while we were busily scheduling ourselves. In that case, it could |
| 885 | * be minutes before the service processor comes back, so only try |
| 886 | * again once a second. |
| 887 | */ |
| 888 | schedule_delayed_work(&hp->writer, HZ); |
| 889 | goto out; |
| 890 | } |
| 891 | |
| 892 | hvsi_push(hp); |
| 893 | if (hp->n_outbuf > 0) |
| 894 | schedule_delayed_work(&hp->writer, 10); |
| 895 | else { |
| 896 | #ifdef DEBUG |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 897 | pr_debug("%s: outbuf emptied after %li jiffies\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | jiffies - start_j); |
| 899 | start_j = 0; |
| 900 | #endif /* DEBUG */ |
| 901 | wake_up_all(&hp->emptyq); |
Jiri Slaby | daea440 | 2012-04-02 13:54:30 +0200 | [diff] [blame] | 902 | tty = tty_port_tty_get(&hp->port); |
| 903 | if (tty) { |
| 904 | tty_wakeup(tty); |
| 905 | tty_kref_put(tty); |
| 906 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | out: |
| 910 | spin_unlock_irqrestore(&hp->lock, flags); |
| 911 | } |
| 912 | |
| 913 | static int hvsi_write_room(struct tty_struct *tty) |
| 914 | { |
Alan Cox | c9f19e9 | 2009-01-02 13:47:26 +0000 | [diff] [blame] | 915 | struct hvsi_struct *hp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
| 917 | return N_OUTBUF - hp->n_outbuf; |
| 918 | } |
| 919 | |
| 920 | static int hvsi_chars_in_buffer(struct tty_struct *tty) |
| 921 | { |
Alan Cox | c9f19e9 | 2009-01-02 13:47:26 +0000 | [diff] [blame] | 922 | struct hvsi_struct *hp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | return hp->n_outbuf; |
| 925 | } |
| 926 | |
| 927 | static int hvsi_write(struct tty_struct *tty, |
| 928 | const unsigned char *buf, int count) |
| 929 | { |
| 930 | struct hvsi_struct *hp = tty->driver_data; |
| 931 | const char *source = buf; |
| 932 | unsigned long flags; |
| 933 | int total = 0; |
| 934 | int origcount = count; |
| 935 | |
| 936 | spin_lock_irqsave(&hp->lock, flags); |
| 937 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 938 | pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
| 940 | if (!is_open(hp)) { |
| 941 | /* we're either closing or not yet open; don't accept data */ |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 942 | pr_debug("%s: not open\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | goto out; |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf |
| 948 | * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls |
| 949 | * will see there is no room in outbuf and return. |
| 950 | */ |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 951 | while ((count > 0) && (hvsi_write_room(tty) > 0)) { |
| 952 | int chunksize = min(count, hvsi_write_room(tty)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
| 954 | BUG_ON(hp->n_outbuf < 0); |
| 955 | memcpy(hp->outbuf + hp->n_outbuf, source, chunksize); |
| 956 | hp->n_outbuf += chunksize; |
| 957 | |
| 958 | total += chunksize; |
| 959 | source += chunksize; |
| 960 | count -= chunksize; |
| 961 | hvsi_push(hp); |
| 962 | } |
| 963 | |
| 964 | if (hp->n_outbuf > 0) { |
| 965 | /* |
| 966 | * we weren't able to write it all to the hypervisor. |
| 967 | * schedule another push attempt. |
| 968 | */ |
| 969 | schedule_delayed_work(&hp->writer, 10); |
| 970 | } |
| 971 | |
| 972 | out: |
| 973 | spin_unlock_irqrestore(&hp->lock, flags); |
| 974 | |
| 975 | if (total != origcount) |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 976 | pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | total); |
| 978 | |
| 979 | return total; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * I have never seen throttle or unthrottle called, so this little throttle |
| 984 | * buffering scheme may or may not work. |
| 985 | */ |
| 986 | static void hvsi_throttle(struct tty_struct *tty) |
| 987 | { |
Alan Cox | c9f19e9 | 2009-01-02 13:47:26 +0000 | [diff] [blame] | 988 | struct hvsi_struct *hp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 990 | pr_debug("%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
| 992 | h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); |
| 993 | } |
| 994 | |
| 995 | static void hvsi_unthrottle(struct tty_struct *tty) |
| 996 | { |
Alan Cox | c9f19e9 | 2009-01-02 13:47:26 +0000 | [diff] [blame] | 997 | struct hvsi_struct *hp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 1000 | pr_debug("%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
| 1002 | spin_lock_irqsave(&hp->lock, flags); |
| 1003 | if (hp->n_throttle) { |
Jiri Slaby | 28c0447 | 2012-04-02 13:54:29 +0200 | [diff] [blame] | 1004 | hvsi_send_overflow(hp, tty); |
| 1005 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | } |
| 1007 | spin_unlock_irqrestore(&hp->lock, flags); |
| 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
| 1010 | h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE); |
| 1011 | } |
| 1012 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 1013 | static int hvsi_tiocmget(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | { |
Alan Cox | c9f19e9 | 2009-01-02 13:47:26 +0000 | [diff] [blame] | 1015 | struct hvsi_struct *hp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
| 1017 | hvsi_get_mctrl(hp); |
| 1018 | return hp->mctrl; |
| 1019 | } |
| 1020 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 1021 | static int hvsi_tiocmset(struct tty_struct *tty, |
| 1022 | unsigned int set, unsigned int clear) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | { |
Alan Cox | c9f19e9 | 2009-01-02 13:47:26 +0000 | [diff] [blame] | 1024 | struct hvsi_struct *hp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | unsigned long flags; |
| 1026 | uint16_t new_mctrl; |
| 1027 | |
| 1028 | /* we can only alter DTR */ |
| 1029 | clear &= TIOCM_DTR; |
| 1030 | set &= TIOCM_DTR; |
| 1031 | |
| 1032 | spin_lock_irqsave(&hp->lock, flags); |
| 1033 | |
| 1034 | new_mctrl = (hp->mctrl & ~clear) | set; |
| 1035 | |
| 1036 | if (hp->mctrl != new_mctrl) { |
| 1037 | hvsi_set_mctrl(hp, new_mctrl); |
| 1038 | hp->mctrl = new_mctrl; |
| 1039 | } |
| 1040 | spin_unlock_irqrestore(&hp->lock, flags); |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1046 | static const struct tty_operations hvsi_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | .open = hvsi_open, |
| 1048 | .close = hvsi_close, |
| 1049 | .write = hvsi_write, |
| 1050 | .hangup = hvsi_hangup, |
| 1051 | .write_room = hvsi_write_room, |
| 1052 | .chars_in_buffer = hvsi_chars_in_buffer, |
| 1053 | .throttle = hvsi_throttle, |
| 1054 | .unthrottle = hvsi_unthrottle, |
| 1055 | .tiocmget = hvsi_tiocmget, |
| 1056 | .tiocmset = hvsi_tiocmset, |
| 1057 | }; |
| 1058 | |
| 1059 | static int __init hvsi_init(void) |
| 1060 | { |
| 1061 | int i; |
| 1062 | |
| 1063 | hvsi_driver = alloc_tty_driver(hvsi_count); |
| 1064 | if (!hvsi_driver) |
| 1065 | return -ENOMEM; |
| 1066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | hvsi_driver->driver_name = "hvsi"; |
| 1068 | hvsi_driver->name = "hvsi"; |
| 1069 | hvsi_driver->major = HVSI_MAJOR; |
| 1070 | hvsi_driver->minor_start = HVSI_MINOR; |
| 1071 | hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM; |
| 1072 | hvsi_driver->init_termios = tty_std_termios; |
| 1073 | hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1074 | hvsi_driver->init_termios.c_ispeed = 9600; |
| 1075 | hvsi_driver->init_termios.c_ospeed = 9600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | hvsi_driver->flags = TTY_DRIVER_REAL_RAW; |
| 1077 | tty_set_operations(hvsi_driver, &hvsi_ops); |
| 1078 | |
| 1079 | for (i=0; i < hvsi_count; i++) { |
| 1080 | struct hvsi_struct *hp = &hvsi_ports[i]; |
| 1081 | int ret = 1; |
| 1082 | |
Jiri Slaby | b19e2ca | 2012-08-07 21:47:51 +0200 | [diff] [blame] | 1083 | tty_port_link_device(&hp->port, hvsi_driver, i); |
| 1084 | |
Yong Zhang | 9cfb5c0 | 2011-09-22 16:59:15 +0800 | [diff] [blame] | 1085 | ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | if (ret) |
| 1087 | printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n", |
| 1088 | hp->virq, ret); |
| 1089 | } |
| 1090 | hvsi_wait = wait_for_state; /* irqs active now */ |
| 1091 | |
| 1092 | if (tty_register_driver(hvsi_driver)) |
| 1093 | panic("Couldn't register hvsi console driver\n"); |
| 1094 | |
Olof Johansson | 8b6a7b2 | 2006-04-12 15:19:50 -0500 | [diff] [blame] | 1095 | printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | device_initcall(hvsi_init); |
| 1100 | |
| 1101 | /***** console (not tty) code: *****/ |
| 1102 | |
| 1103 | static void hvsi_console_print(struct console *console, const char *buf, |
| 1104 | unsigned int count) |
| 1105 | { |
| 1106 | struct hvsi_struct *hp = &hvsi_ports[console->index]; |
| 1107 | char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__; |
| 1108 | unsigned int i = 0, n = 0; |
| 1109 | int ret, donecr = 0; |
| 1110 | |
| 1111 | mb(); |
| 1112 | if (!is_open(hp)) |
| 1113 | return; |
| 1114 | |
| 1115 | /* |
| 1116 | * ugh, we have to translate LF -> CRLF ourselves, in place. |
| 1117 | * copied from hvc_console.c: |
| 1118 | */ |
| 1119 | while (count > 0 || i > 0) { |
| 1120 | if (count > 0 && i < sizeof(c)) { |
| 1121 | if (buf[n] == '\n' && !donecr) { |
| 1122 | c[i++] = '\r'; |
| 1123 | donecr = 1; |
| 1124 | } else { |
| 1125 | c[i++] = buf[n++]; |
| 1126 | donecr = 0; |
| 1127 | --count; |
| 1128 | } |
| 1129 | } else { |
| 1130 | ret = hvsi_put_chars(hp, c, i); |
| 1131 | if (ret < 0) |
| 1132 | i = 0; |
| 1133 | i -= ret; |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | static struct tty_driver *hvsi_console_device(struct console *console, |
| 1139 | int *index) |
| 1140 | { |
| 1141 | *index = console->index; |
| 1142 | return hvsi_driver; |
| 1143 | } |
| 1144 | |
| 1145 | static int __init hvsi_console_setup(struct console *console, char *options) |
| 1146 | { |
Roel Kluin | 7286565 | 2009-08-06 13:00:37 +0000 | [diff] [blame] | 1147 | struct hvsi_struct *hp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | int ret; |
| 1149 | |
| 1150 | if (console->index < 0 || console->index >= hvsi_count) |
| 1151 | return -1; |
Roel Kluin | 7286565 | 2009-08-06 13:00:37 +0000 | [diff] [blame] | 1152 | hp = &hvsi_ports[console->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | |
| 1154 | /* give the FSP a chance to change the baud rate when we re-open */ |
| 1155 | hvsi_close_protocol(hp); |
| 1156 | |
| 1157 | ret = hvsi_handshake(hp); |
| 1158 | if (ret < 0) |
| 1159 | return ret; |
| 1160 | |
| 1161 | ret = hvsi_get_mctrl(hp); |
| 1162 | if (ret < 0) |
| 1163 | return ret; |
| 1164 | |
| 1165 | ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR); |
| 1166 | if (ret < 0) |
| 1167 | return ret; |
| 1168 | |
| 1169 | hp->flags |= HVSI_CONSOLE; |
| 1170 | |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
Chris Metcalf | 4455b11 | 2010-07-06 08:03:16 +0000 | [diff] [blame] | 1174 | static struct console hvsi_console = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | .name = "hvsi", |
| 1176 | .write = hvsi_console_print, |
| 1177 | .device = hvsi_console_device, |
| 1178 | .setup = hvsi_console_setup, |
| 1179 | .flags = CON_PRINTBUFFER, |
| 1180 | .index = -1, |
| 1181 | }; |
| 1182 | |
| 1183 | static int __init hvsi_console_init(void) |
| 1184 | { |
| 1185 | struct device_node *vty; |
| 1186 | |
| 1187 | hvsi_wait = poll_for_state; /* no irqs yet; must poll */ |
| 1188 | |
| 1189 | /* search device tree for vty nodes */ |
| 1190 | for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol"); |
| 1191 | vty != NULL; |
| 1192 | vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) { |
| 1193 | struct hvsi_struct *hp; |
Jeremy Kerr | 954a46e | 2006-07-12 15:39:43 +1000 | [diff] [blame] | 1194 | const uint32_t *vtermno, *irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 1196 | vtermno = of_get_property(vty, "reg", NULL); |
| 1197 | irq = of_get_property(vty, "interrupts", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | if (!vtermno || !irq) |
| 1199 | continue; |
| 1200 | |
| 1201 | if (hvsi_count >= MAX_NR_HVSI_CONSOLES) { |
| 1202 | of_node_put(vty); |
| 1203 | break; |
| 1204 | } |
| 1205 | |
| 1206 | hp = &hvsi_ports[hvsi_count]; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1207 | INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker); |
| 1208 | INIT_WORK(&hp->handshaker, hvsi_handshaker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | init_waitqueue_head(&hp->emptyq); |
| 1210 | init_waitqueue_head(&hp->stateq); |
| 1211 | spin_lock_init(&hp->lock); |
Jiri Slaby | d73a4e7 | 2012-04-02 13:54:28 +0200 | [diff] [blame] | 1212 | tty_port_init(&hp->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | hp->index = hvsi_count; |
| 1214 | hp->inbuf_end = hp->inbuf; |
| 1215 | hp->state = HVSI_CLOSED; |
| 1216 | hp->vtermno = *vtermno; |
Benjamin Herrenschmidt | 6e99e45 | 2006-07-10 04:44:42 -0700 | [diff] [blame] | 1217 | hp->virq = irq_create_mapping(NULL, irq[0]); |
Alan Cox | d4e33fa | 2012-01-26 17:44:09 +0000 | [diff] [blame] | 1218 | if (hp->virq == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n", |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 1220 | __func__, irq[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | continue; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 1222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
| 1224 | hvsi_count++; |
| 1225 | } |
| 1226 | |
| 1227 | if (hvsi_count) |
Chris Metcalf | 4455b11 | 2010-07-06 08:03:16 +0000 | [diff] [blame] | 1228 | register_console(&hvsi_console); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | return 0; |
| 1230 | } |
| 1231 | console_initcall(hvsi_console_init); |