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