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